Maven配置
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.55</version>
</dependency>
编写工具类
public ChannelSftp getChannel(Map<String, String> sftpDetails, int timeout) throws JSchException {
String ftpHost = sftpDetails.get(SFTPConstants.SFTP_REQ_HOST);
String port = sftpDetails.get(SFTPConstants.SFTP_REQ_PORT);
String ftpUserName = sftpDetails.get(SFTPConstants.SFTP_REQ_USERNAME);
String ftpPassword = sftpDetails.get(SFTPConstants.SFTP_REQ_PASSWORD);
int ftpPort = SFTPConstants.SFTP_DEFAULT_PORT;
if (port != null && !port.equals("")) {
ftpPort = Integer.valueOf(port);
}
JSch jsch = new JSch(); // 创建JSch对象
session = jsch.getSession(ftpUserName, ftpHost, ftpPort); // 根据用户名,主机ip,端口获取一个Session对象
if (ftpPassword != null) {
session.setPassword(ftpPassword); // 设置密码
}
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");//设置不用检查HostKey,设成yes,一旦计算机的密匙发生变化,就拒绝连接。
session.setConfig(config); // 为Session对象设置properties
session.setTimeout(timeout); // 设置timeout时间
session.connect(); // 通过Session建立链接
channel = session.openChannel("sftp"); // 打开SFTP通道
channel.connect(); // 建立SFTP通道的连接
LOG.info("Connected successfully to ftpHost = " + ftpHost + ",as ftpUserName = " + ftpUserName
+ ", returning: " + channel);
return (ChannelSftp) channel;
}
public void closeChannel() throws Exception {
if (channel != null) {
channel.disconnect();
}
if (session != null) {
session.disconnect();
}
}
文件下载
public void downloadFile() {
SFTPChannel channel = null;
ChannelSftp chSftp = null;
try {
Map<String, String> sftpDetails = new HashMap<String, String>();
sftpDetails.put(SFTPConstants.SFTP_REQ_HOST, "94.191.117.99");
sftpDetails.put(SFTPConstants.SFTP_REQ_USERNAME, "ddkx");
sftpDetails.put(SFTPConstants.SFTP_REQ_PASSWORD, "ddkx");
sftpDetails.put(SFTPConstants.SFTP_REQ_PORT, "26");
channel = new SFTPChannel();
chSftp = channel.getChannel(sftpDetails, 60000);
String downPath = "D:" + File.separator + "ddrbdnl";
File localFile = new File(downPath);
if (!localFile.exists()) {
localFile.mkdirs();
}
String filename = "sftp.txt";
chSftp.get(filename, downPath);
log.info("------文件下载成功------");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
chSftp.quit();
channel.closeChannel();
} catch (Exception e) {
e.printStackTrace();
}
}
}
文件上传
public static void main(String[] args) throws Exception {
SFTPTest test = new SFTPTest();
Map<String, String> sftpDetails = new HashMap<String, String>();
// 设置主机ip,端口,用户名,密码
sftpDetails.put(SFTPConstants.SFTP_REQ_HOST, "10.9.167.55");
sftpDetails.put(SFTPConstants.SFTP_REQ_USERNAME, "root");
sftpDetails.put(SFTPConstants.SFTP_REQ_PASSWORD, "arthur");
sftpDetails.put(SFTPConstants.SFTP_REQ_PORT, "22");
String src = "D:\\DevSoft\\HB-SnagIt1001.rar"; // 本地文件名
String dst = "/home/omc/ylong/sftp/HB-SnagIt1001.rar"; // 目标文件名
SFTPChannel channel = test.getSFTPChannel();
ChannelSftp chSftp = channel.getChannel(sftpDetails, 60000);
/**
* 代码段1
OutputStream out = chSftp.put(dst, ChannelSftp.OVERWRITE); // 使用OVERWRITE模式
byte[] buff = new byte[1024 * 256]; // 设定每次传输的数据块大小为256KB
int read;
if (out != null) {
System.out.println("Start to read input stream");
InputStream is = new FileInputStream(src);
do {
read = is.read(buff, 0, buff.length);
if (read > 0) {
out.write(buff, 0, read);
}
out.flush();
} while (read >= 0);
System.out.println("input stream read done.");
}
**/
chSftp.put(src, dst, ChannelSftp.OVERWRITE); // 代码段2
// chSftp.put(new FileInputStream(src), dst, ChannelSftp.OVERWRITE); // 代码段3
chSftp.quit();
channel.closeChannel();
}
监控传输进度
public interface SftpProgressMonitor{
public static final int PUT=0;
public static final int GET=1;
void init(int op, String src, String dest, long max);
boolean count(long count);
void end();
}
实现类1
public class MyProgressMonitor implements SftpProgressMonitor {
private long transfered;
实现类2
public class FileProgressMonitor extends TimerTask implements SftpProgressMonitor {
private long progressInterval = 5 * 1000; // 默认间隔时间为5秒
private boolean isEnd = false; // 记录传输是否结束
private long transfered; // 记录已传输的数据总大小
private long fileSize; // 记录文件总大小
private Timer timer; // 定时器对象
private boolean isScheduled = false; // 记录是否已启动timer记时器
public FileProgressMonitor(long fileSize) {
this.fileSize = fileSize;
}