依赖的jar包:com.jcraft:jsch:0.1.54
org.apache.commons:commons-lang3:3.4
commons-net:commons-net:3.3
public class SftpUtil {
private SftpUtil(){}
public static void uploadSftpFile(String ftpHost, int ftpPort, String ftpUserName, String remoteFilepath, String localFilePath, String privateKey) throws JSchException, SftpException {
Session session = getSftpSession(ftpHost, ftpPort, ftpUserName, null, privateKey);
uploadSftpFile(remoteFilepath, localFilePath, session);
}
public static void uploadSftpFile(int ftpPort, String ftpHost, String ftpUserName, String remoteFilepath, String localFilePath, String password) throws JSchException, SftpException {
Session session = getSftpSession(ftpHost, ftpPort, ftpUserName, password, null);
uploadSftpFile(remoteFilepath, localFilePath, session);
}
private static void uploadSftpFile(String remoteFilepath, String localFilePath, Session session) throws JSchException, SftpException {
Channel channel = session.openChannel("sftp");
channel.connect();
ChannelSftp chSftp = (ChannelSftp) channel;
try {
chSftp.put(localFilePath, remoteFilepath);
} finally {
chSftp.quit();
channel.disconnect();
session.disconnect();
}
}
public static void uploadFtpFile(int ftpPort, String ftpHost, String ftpUserName, String remoteFilepath, String localFilePath, String password) throws JSchException, SftpException, IOException {
FTPClient ftpClient = null;
InputStream in = null;
try {
ftpClient = getFtpClient(ftpHost, ftpPort, ftpUserName, password);
ftpClient.setControlEncoding("UTF-8"); // 中文支持
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
ftpClient.enterLocalPassiveMode();
in = new FileInputStream(localFilePath);
boolean storeFile = ftpClient.storeFile(remoteFilepath, in);
if(!storeFile) {
in.close();
ftpClient.disconnect();
throw new PosloanException("-999", "upload failed!");
}
} finally {
if(in != null) in.close();
if(ftpClient != null)
ftpClient.disconnect();
}
}
/**
* 通过sftp下载文件数据
* @param ftpHost
* @param ftpUserName
* @param ftpPort
* @param remoteFile
* @param localFilePath
* @throws JSchException
* @throws SftpException
*/
public static void downloadSftpFile(String ftpHost, int ftpPort, String ftpUserName, String remoteFilepath, String localFilePath, String privateKey) throws JSchException, SftpException {
Session session = getSftpSession(ftpHost, ftpPort, ftpUserName, null, privateKey);
downloadSftpFile(remoteFilepath, localFilePath, session);
}
public static void downloadSftpFile(int ftpPort, String ftpHost, String ftpUserName, String remoteFilepath, String localFilePath, String password) throws JSchException, SftpException {
Session session = getSftpSession(ftpHost, ftpPort, ftpUserName, password, null);
downloadSftpFile(remoteFilepath, localFilePath, session);
}
/**
* 通过ftp下载文件数据
* @param ftpPort
* @param ftpHost
* @param ftpUserName
* @param remoteFilepath
* @param localFilePath
* @param password
* @throws JSchException
* @throws SftpException
* @throws IOException
*/
public static void downloadFtpFile(int ftpPort, String ftpHost, String ftpUserName, String remoteFilepath, String localFilePath, String password) throws JSchException, SftpException, IOException, PosloanException {
FTPClient ftpClient = null;
OutputStream out = null;
try {
ftpClient = getFtpClient(ftpHost, ftpPort, ftpUserName, password);
FTPFile[] listFiles = ftpClient.listFiles(remoteFilepath);
if(listFiles.length == 0) {
throw new PosloanException("", String.format("下载文件不存在,文件路径:%s", remoteFilepath));
}
out =new FileOutputStream(localFilePath);
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
ftpClient.retrieveFile(remoteFilepath, out);
} finally {
if(out != null) out.close();
if(ftpClient != null) ftpClient.disconnect();
}
}
/**
* 下载文件
* */
private static void downloadSftpFile(String remoteFilepath, String localFilePath, Session session) throws JSchException, SftpException {
Channel channel = session.openChannel("sftp");
channel.connect();
ChannelSftp chSftp = (ChannelSftp) channel;
try {
if (remoteFilepath.startsWith("/")) {
chSftp.cd("/");
}
chSftp.get(remoteFilepath, localFilePath);
} finally {
chSftp.quit();
channel.disconnect();
session.disconnect();
}
}
/**
* 获取sftp连接session
* */
private static Session getSftpSession(String ftpHost, int ftpPort, String ftpUserName, String password, String privateKey) throws JSchException {
JSch jsch = new JSch();
if(StringUtils.isNotBlank(privateKey)) {
jsch.addIdentity(privateKey);
}
Session session = jsch.getSession(ftpUserName, ftpHost, ftpPort);
session.setTimeout(100000);
if(StringUtils.isNotBlank(password)) {
session.setPassword(password);
}
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
return session;
}
/**
* 获取ftp客服端
*/
private static FTPClient getFtpClient(String ftpHost, int ftpPort, String ftpUserName, String password) throws IOException {
FTPClient ftpClient = new FTPClient();
ftpClient.connect(ftpHost,ftpPort);
ftpClient.login(ftpUserName, password);
int reply = ftpClient.getReplyCode();
ftpClient.setDataTimeout(60000);
if (!FTPReply.isPositiveCompletion(reply)) {
ftpClient.disconnect();
throw new PosloanException("-999", "FTP server refuse connect!");
}
return ftpClient;
}
}