SFT 工具类 package com.jyonlinesoa.util;import com.jcraft.jsch.*;import java.util.Properties;/** * Description: SFTP 工具类 * Created by JungLone on 2017/10/12 17:32. */public class SFTPUtils { private String strHost; private int nPor
package com.jyonlinesoa.util;
import com.jcraft.jsch.*;
import java.util.Properties;
/**
* Description: SFTP 工具类
* Created by JungLone on 2017/10/12 17:32.
*/
public class SFTPUtils {
private String strHost;
private int nPort;
private String strUserName;
private String strPassword;
private Session sshSession = null;
private ChannelSftp channelSftp = null;
/**
* Description: 构造 SFTPUtils
*
* @param strHost host
* @param nPort 端口
* @param strUserName 用户名
* @param strPassword 密码
*/
public SFTPUtils(String strHost, int nPort, String strUserName, String strPassword) {
this.strHost = strHost;
this.nPort = nPort;
this.strUserName = strUserName;
this.strPassword = strPassword;
}
/**
* Description: 创建连接
*/
private void connect() {
JSch jsch = new JSch();
try {
sshSession = jsch.getSession(this.strUserName, this.strHost, this.nPort);
sshSession.setPassword(strPassword);
Properties sshConfig = new Properties();
sshConfig.put("StrictHostKeyChecking", "no");
sshSession.setConfig(sshConfig);
sshSession.connect();
Channel channel = sshSession.openChannel("sftp");
channel.connect();
channelSftp = (ChannelSftp) channel;
} catch (JSchException e) {
e.printStackTrace();
}
}
/**
* Description: 断开连接
*/
private void disConnect() {
if (null != sshSession && sshSession.isConnected()) {
sshSession.disconnect();
}
if (null != channelSftp && channelSftp.isConnected()) {
channelSftp.disconnect();
}
}
/**
* Description: 上件文件到远程服务器
*
* @param strRemoteDir 远程服务器目录
* @param strFileName 文件名
* @param strLocalDir 本地目录
*/
public void upload(String strRemoteDir, String strFileName, String strLocalDir) {
try {
this.connect();
this.channelSftp.cd(strRemoteDir);
String strLocalFileName = this.getLocalFileName(strLocalDir, strFileName);
this.channelSftp.put(strLocalFileName, strFileName);
} catch (Exception e) {
e.printStackTrace();
} finally {
this.disConnect();
}
}
/**
* Description: 下载文件
*
* @param strRemoteDir 远程服务器目录
* @param strFileName 文件名
* @param strLocalDir 本地目录
*/
public void download(String strRemoteDir, String strFileName, String strLocalDir) {
try {
this.connect();
this.channelSftp.cd(strRemoteDir);
this.channelSftp.get(strFileName, this.getLocalFileName(strLocalDir, strFileName));
} catch (Exception e) {
e.printStackTrace();
} finally {
this.disConnect();
}
}
/**
* Description: 取得文件全路径
*
* @param strLocalDir 本地目录
* @param strFileName 文件名
* @return 文件全路径
*/
private String getLocalFileName(String strLocalDir, String strFileName) {
String strLocalFileName;
if (strLocalDir.endsWith("/")) {
strLocalFileName = strLocalDir + strFileName;
} else {
strLocalFileName = strLocalDir + "/" + strFileName;
}
return strLocalFileName;
}
public static void main(String[] args) {
String strHost = "ftp-1.com";
int nPort = 22;
String strUserName = "353030sftp";
String strPassword = "g2s4T5vb2ry2uo";
SFTPUtils sftpUtils = new SFTPUtils(strHost, nPort, strUserName, strPassword);
String strRemoteDir = "check";
String strFileName = "new_test_check_2017101217.txt";
String strLocalDir = "/Users/junglone";
// sftpUtils.download(strDir, strFileName, strLocalDir);
sftpUtils.upload(strRemoteDir, strFileName, strLocalDir);
}
}
