工具类-- SFTP文件下载和上传
下载功能–下载到本地文件夹中,并将SFTP文件删除备份到备份文件夹中.
上传功能–就是上传.
package com.util;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.util.Iterator;
import java.util.Vector;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;
import com.jcraft.jsch.UserInfo;
/**
* SFTP工具类
*/
public class SFTPUtils{
private static final Logger log = LoggerFactory.getLogger(SFTPUtils.class);
/**
* 上传数据信息到SFTP
* @param host
* @param port 端口号
* @param userName 用户名
* @param password 密码
* @param keyFile 密钥
* @param filePath 文件路径
* @param remoteAbsolutePath 远程路径
* @param @throws IOException
*/
public static boolean uploadSFTP(String host,String port,String userName,String password,String keyFile,String filePath,String remoteAbsolutePath) throws IOException{
JSch jsch = new JSch();
try {
String resource = SFTPUtils.class.getResource("/")+keyFile;
String keyFilePath = resource.replace("file:", "");
jsch.addIdentity(keyFilePath);
Session sesison = jsch.getSession(userName, host ,Integer.parseInt(port));
UserInfo info = new MyUserInfo(password);
sesison.setUserInfo(info);
sesison.connect();
Channel channel = sesison.openChannel("sftp");
channel.connect();
ChannelSftp sftp = (ChannelSftp) channel;
try {
//sftp.mkdir("/nike/test");
sftp.put(filePath, remoteAbsolutePath);
//sftp.put(new FileInputStream(new File(filePath)), remoteAbsolutePath);
} catch (SftpException e) {
log.error("上传数据失败,报错原因为 "+e);
return false;
}finally{
//退出
sftp.disconnect();
}
} catch (JSchException e) {
e.printStackTrace();
return false;
}finally{
try {
File tempFile = new File(filePath);
if(tempFile!=null)
tempFile.delete();
} catch (Exception e) {
e.printStackTrace();
}
}
return true;
}
public static class MyUserInfo implements UserInfo {
private String passPhrase;
public MyUserInfo(String passPhrase) {
super();
this.passPhrase = passPhrase;
}
@Override
public String getPassphrase() {
// TODO Auto-generated method stub
return passPhrase;
}
@Override
public String getPassword() {
// TODO Auto-generated method stub
return null;
}
@Override
public boolean promptPassword(String message) {
// TODO Auto-generated method stub
return true;
}
@Override
public boolean promptPassphrase(String message) {
// TODO Auto-generated method stub
return true;
}
@Override
public boolean promptYesNo(String message) {
// TODO Auto-generated method stub
return true;
}
@Override
public void showMessage(String message) {
//System.out.println(message);
}
}
/**
* SFTP下载数据到临时文件夹,并备份文件到SFTP备份文件夹中.
* @param host
* @param port 端口号
* @param userName 用户名
* @param password 密码
* @param keyFile 密钥
* @param localFilePath 本地临时文件路径
* @param remoteAbsolutePath 远程路径
* @param remoteBakPath 远程备份路径
* @Author 楚长川
*/
public static boolean downloadSFTP(String host,String port,String userName,String password,String keyFile,String localFilePath ,String remoteAbsolutePath,String remoteBakPath) throws IOException{
JSch jsch = new JSch();
try {
String resource = String.valueOf(Thread.currentThread().getContextClassLoader().getResource(keyFile));
String keyFilePath = resource.replace("file:", "");
jsch.addIdentity(keyFilePath);
Session sesison = jsch.getSession(userName, host ,Integer.parseInt(port));
UserInfo info = new MyUserInfo(password);
sesison.setUserInfo(info);
sesison.connect();
Channel channel = sesison.openChannel("sftp");
channel.connect();
ChannelSftp sftp = (ChannelSftp) channel;
try {
Vector sftpFiles = sftp.ls(remoteAbsolutePath);
if(sftpFiles.size()>0){
Iterator sftpIts = sftpFiles.iterator();
while(sftpIts.hasNext()){
ChannelSftp.LsEntry entry = (ChannelSftp.LsEntry)sftpIts.next();
String filename = entry.getFilename();
if(filename.equals("..")||filename.equals(".")) {
continue;
}
sftp.get(remoteAbsolutePath + filename, localFilePath);
//下载完成后,将下载文件上传至备份文件夹
sftp.put(localFilePath + File.separator + filename, remoteBakPath);
//删除读取文件,防止二次读取
sftp.rm(remoteAbsolutePath+filename);
}
}
} catch (SftpException e) {
log.error("下载数据失败,报错原因为 "+e);
return false;
}finally{
//退出
sftp.disconnect();
}
} catch (JSchException e) {
e.printStackTrace();
return false;
}
return true;
}
}