1.配置
首先需要导入jar包jsch.jar
public class FileConfig {
/**
* SFTP操作文件服务器配置部分
* @author tian
*/
public static String SFTP_Host="192.168.**.**";//主机
public static String SFTP_Username="**";
public static String SFTP_Pass="****";
public static int SFTP_Port=22;//端口
public static String SFTP_UploadPath="/usr/data/img";//SFTP上传的远程主机文件路径
public static String tempFilePath="/usr/local/temp";//附件上传,下载,暂存路径
}
2.实现方法
public class FileSFTP {
private String host;//主机
private String username;
private String password;
private int port;//端口
private ChannelSftp sftp = null;
private Session sshSession = null;
public FileSFTP() {}
public FileSFTP(String host, String username, String password, int port) {
this.host = host;
this.username = username;
this.password = password;
this.port = port;
}
/**
*连接sftp服务器
*/
public void connect() {
try {
JSch jsch = new JSch();
jsch.getSession(username, host, port);
sshSession = jsch.getSession(username, host, port);
sshSession.setPassword(password);
Properties sshConfig = new Properties();
sshConfig.put("StrictHostKeyChecking", "no");
sshSession.setConfig(sshConfig);
sshSession.connect();
Channel channel = sshSession.openChannel("sftp");
channel.connect();
sftp = (ChannelSftp) channel;
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 关闭资源
*/
public void disconnect() {
if (this.sftp != null) {
if (this.sftp.isConnected()) {
this.sftp.disconnect();
}
}
if (this.sshSession != null) {
if (this.sshSession.isConnected()) {
this.sshSession.disconnect();
}
}
}
/**
* 批量下载文件
*
* @param remotPath
* 远程下载目录(以路径符号结束)
* @param localPath
* 本地保存目录(以路径符号结束)
* @param fileFormat
* 下载文件格式(以特定字符开头,为空不做检验)
* @param del
* 下载后是否删除sftp文件
* @return
*/
@SuppressWarnings("rawtypes")
public boolean batchDownLoadFile(String remotPath, String localPath,
String fileFormat, boolean del) {
try {
connect();
Vector v = listFiles(remotPath);
if (v.size() > 0) {
Iterator it = v.iterator();
while (it.hasNext()) {
LsEntry entry = (LsEntry) it.next();
String filename = entry.getFilename();
SftpATTRS attrs = entry.getAttrs();
if (!attrs.isDir()) {
if (fileFormat != null && !"".equals(fileFormat.trim())) {
if (filename.startsWith(fileFormat)) {
if (this.downloadFile(remotPath, filename,
localPath, filename)
&& del) {
deleteSFTP(remotPath, filename);
}
}
} else {
if (this.downloadFile(remotPath, filename,
localPath, filename)
&& del) {
deleteSFTP(remotPath, filename);
}
}
}
}
}
} catch (SftpException e) {
e.printStackTrace();
} finally {
this.disconnect();
}
return false;
}
/**
* 下载单个文件
* @param remotPath
* 远程下载目录(以路径符号结束)
* @param remoteFileName
* 下载文件名
* @param localPath
* 本地保存目录(以路径符号结束)
* @param localFileName
* 保存文件名
* @return
*/
public boolean downloadFile(String remotePath, String remoteFileName,
String localPath, String localFileName) {
try {
sftp.cd(remotePath);
File file = new File(localPath +"/"+ localFileName);
mkdirs(localPath);
sftp.get(remoteFileName, new FileOutputStream(file));
return true;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (SftpException e) {
e.printStackTrace();
}
return false;
}
/**
* 上传单个文件
*
* @param remotePath
* 远程保存目录
* @param remoteFileName
* 保存文件名
* @param localPath
* 本地上传目录
* @param localFileName
* 上传的文件名
* @return
*/
public boolean uploadFile(String remotePath, String remoteFileName,
String localPath, String localFileName) {
FileInputStream in = null;
try {
this.createDir(remotePath);
File file = new File(localPath +File.separatorChar+ localFileName);
in = new FileInputStream(file);
sftp.put(in, remoteFileName);
return true;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (SftpException e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return false;
}
/**
* 批量上传文件
*
* @param remotePath
* 远程保存目录
* @param localPath
* 本地上传目录(以路径符号结束)
* @param del
* 上传后是否删除本地文件
* @return
*/
public boolean bacthUploadFile(String remotePath, String localPath,
boolean del) {
try {
connect();
File file = new File(localPath);
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++) {
if (files[i].isFile()
&& files[i].getName().indexOf("bak") == -1) {
if (this.uploadFile(remotePath, files[i].getName(),
localPath, files[i].getName())
&& del) {
deleteFile(localPath + files[i].getName());
}
}
}
return true;
} catch (Exception e) {
e.printStackTrace();
} finally {
this.disconnect();
}
return false;
}
/**
* 删除本地文件
*
* @param filePath
* @return
*/
public boolean deleteFile(String filePath) {
File file = new File(filePath);
if (!file.isFile()||!file.exists()) {
return false;
}
return file.delete();
}
/**
* 创建目录
*
* @param createpath
* @return
*/
public boolean createDir(String createpath) {
try {
if (this.isDirExist(createpath)) {
this.sftp.cd(createpath);
return true;
}
String pathArry[] = createpath.split("/");
StringBuffer filePath = new StringBuffer("/");
for (String path : pathArry) {
if (path.equals("")) {
continue;
}
filePath.append(path + "/");
if (this.isDirExist(filePath.toString())) {
sftp.cd(filePath.toString());
} else {
// 建立目录
sftp.mkdir(filePath.toString());
// 进入并设置为当前目录
sftp.cd(filePath.toString());
}
}
this.sftp.cd(createpath);
return true;
} catch (SftpException e) {
e.printStackTrace();
}
return false;
}
/**
* 判断目录是否存在
*
* @param directory
* @return
*/
public boolean isDirExist(String directory) {
boolean isDirExistFlag = false;
try {
SftpATTRS sftpATTRS = sftp.lstat(directory);
isDirExistFlag = true;
return sftpATTRS.isDir();
} catch (Exception e) {
if (e.getMessage().toLowerCase().equals("no such file")) {
isDirExistFlag = false;
}
}
return isDirExistFlag;
}
/**
* 删除stfp文件
*
* @param directory
* 要删除文件所在目录
* @param deleteFile
* 要删除的文件
* @param sftp
*/
public void deleteSFTP(String directory, String deleteFile) {
try {
sftp.cd(directory);
sftp.rm(deleteFile);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 如果目录不存在就创建目录
*
* @param path
*/
public void mkdirs(String path) {
File f = new File(path);
if (!f.exists()) {
f.mkdirs();
}
}
/**
* 列出目录下的文件
*
* @param directory
* 要列出的目录
* @param sftp
* @return
* @throws SftpException
*/
@SuppressWarnings("rawtypes")
public Vector listFiles(String directory) throws SftpException {
return sftp.ls(directory);
}
//测试
public static void main(String[] args) {
FileSFTP ftp = new FileSFTP(FileConfig.SFTP_Host, FileConfig.SFTP_Username, FileConfig.SFTP_Pass,FileConfig.SFTP_Port);
ftp.connect();
ftp.downloadFile(FileConfig.SFTP_UploadPath+"/upload/attachment/201402", "text.txt", "C:\\nihao", "test3.txt");
// ftp.bacthUploadFile(remotePath,localPath,true);
// ftp.downloadFile(remotePath, "test.txt", localPath, "test.txt");
// ftp.batchDownLoadFile(remotePath, localPath, null, true);
ftp.disconnect();
System.exit(0);
}
}
来源:oschina
链接:https://my.oschina.net/u/1274137/blog/302527