SFTPUtils.java

import com.jcraft.jsch.*;
import com.jcraft.jsch.ChannelSftp.LsEntry;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component; import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Properties;
import java.util.Vector; @Component
@Slf4j
public class SFTPUtils {
private SFTPUtils instance = null;
private ChannelSftp sftp; public File downloadFtpFile(String ftpHost,String ftpUserName,String ftpPassword,int ftpPort,String ftpPath,String localPath, String fileName, String downName){
if(null==instance){
instance=new SFTPUtils();
}
//获取SFTP连接
sftp = instance.connect(ftpHost, ftpPort, ftpUserName,ftpPassword);
//SFTP上下载文件
return instance.download(ftpPath+fileName, localPath+downName);
}
/**
* 连接sftp服务器
*
* @param host 主机
* @param port 端口
* @param username 用户名
* @param password 密码
* @return
*/
public ChannelSftp connect(String host, int port, String username, String password) {
ChannelSftp sftp = null;
try {
JSch jsch = new JSch();
jsch.getSession(username, host, port);
Session sshSession = jsch.getSession(username, host, port);
sshSession.setPassword(password);
Properties sshConfig = new Properties();
sshConfig.put("StrictHostKeyChecking", "no");
sshSession.setConfig(sshConfig);
sshSession.connect();
log.info("SFTP Session connected.");
Channel channel = sshSession.openChannel("sftp");
channel.connect();
sftp = (ChannelSftp) channel;
log.info("Connected to " + host);
} catch (Exception e) {
log.error(e.getMessage());
}
return sftp;
} /**
* 上传文件
*
* @param directory 上传的目录
* @param uploadFile 要上传的文件
*/
public boolean upload(String directory, String uploadFile) {
try {
sftp.cd(directory);
File file = new File(uploadFile);
FileInputStream fileInputStream = new FileInputStream(file);
sftp.put(fileInputStream, file.getName());
fileInputStream.close();
return true;
} catch (Exception e) {
log.error(e.getMessage());
return false;
}
} /**
* 下载文件
*
* @param directory 下载目录
* @param downloadFile 下载的文件
* @param saveFile 存在本地的路径
*/
public File download(String directory, String downloadFile, String saveFile) {
try {
sftp.cd(directory);
File file = new File(saveFile);
FileOutputStream fileOutputStream = new FileOutputStream(file);
sftp.get(downloadFile, fileOutputStream);
fileOutputStream.close();
return file;
} catch (Exception e) {
log.error(e.getMessage());
return null;
}
} /**
* 下载文件
*
* @param downloadFilePath 下载的文件完整目录
* @param saveFile 存在本地的路径
*/
public File download(String downloadFilePath, String saveFile) {
try {
int i = downloadFilePath.lastIndexOf('/');
if (i == -1)
return null;
sftp.cd(downloadFilePath.substring(0, i));
File file = new File(saveFile);
FileOutputStream fileOutputStream = new FileOutputStream(file);
sftp.get(downloadFilePath.substring(i + 1), fileOutputStream);
fileOutputStream.close();
return file;
} catch (Exception e) {
log.error(e.getMessage());
return null;
}
} /**
* 删除文件
*
* @param directory 要删除文件所在目录
* @param deleteFile 要删除的文件
*/
public void delete(String directory, String deleteFile) {
try {
sftp.cd(directory);
sftp.rm(deleteFile);
} catch (Exception e) {
log.error(e.getMessage());
}
} /**
* 断开连接
*/
public void disconnect() {
try {
sftp.getSession().disconnect();
} catch (JSchException e) {
log.error(e.getMessage());
}
sftp.quit();
sftp.disconnect();
} /**
* 列出目录下的文件
*
* @param directory 要列出的目录
* @throws SftpException
*/
@SuppressWarnings("unused")
public Vector<LsEntry> listFiles(String directory) throws SftpException {
return sftp.ls(directory);
}
}

最新文章

  1. Gradle 实现 Android 多渠道定制化打包
  2. 简明 Git 命令速查表(中文版)
  3. IOS对.Net返回的Base64string解析问题
  4. WP7开发 Sqlite数据库的使用 解决Unable open the database
  5. JsCss笔记
  6. 菜鸟-手把手教你把Acegi应用到实际项目中(10)-保护业务方法
  7. 编写一个Java应用程序,设计一个汽车类Vehicle,包含的属性有车轮个数 wheels和车重weight。小车类Car是Vehicle的子类,其中包含的属性有载人数 loader。卡车类Truck是Car类的子类,其中包含的属性有载重量payload。每个 类都有构造方法和输出相关数据的方法。最后,写一个测试类来测试这些类的功 能。
  8. java synchronized与volatile的区别
  9. Corel VideoStudio Pro X7(会声会影)
  10. c# 后台get post请求
  11. Android自制浏览器WebView-android学习之旅(64)
  12. Linux基础性笔记
  13. __x__(48)0910第六天__CSS Hack
  14. 解决 js setTimeout 传递带参数的函数无效果
  15. python day27--常用模块 time,random,os,序列化
  16. python---django中序列化
  17. C语言 &#183; 递归倒置字符数组
  18. [目标检测]SSD原理
  19. bzoj 3413: 匹配
  20. linux 模拟生成 CAN 设备

热门文章

  1. SQL Server 2014:为什么会提示“用户登录失败”?
  2. Larave框架下Contracts契约的解析
  3. python做一个简易图片下载工具
  4. 集合类源码(四)Collection之BlockingQueue(ArrayBlockingQueue, DelayQueue, LinkedBlockingQueue)
  5. Docker私有云管理平台————Docker Shipyard
  6. IDEA Rider使用64位IISExpress(3)
  7. Sitecore性化 - 您需要了解的4件事
  8. history路由模式下的nginx配置
  9. JavaScript:计算1在数字中出现的次数
  10. HDU 2007-11 Programming Contest