java远程操作文件服务器(linux),使用sftp协议
版本会持续更新,
当前版本:0.31
版本更新时间:2016-10-13
版本修正说明:
1.修正连接关闭,将关闭的方法改成私有,不允许使用者自行关闭(否则会导致连接池获取错误)
版本会持续更新,
当前版本:0.31
版本更新时间:2016-10-13
版本修正说明:
1.修正连接关闭,将关闭的方法改成私有,不允许使用者自行关闭(否则会导致连接池获取错误)
2.优化删除文件及文件夹,先判断文件或文件夹是否存在,然后再删除
前言:
sftp是Secure File Transfer Protocol的缩写,安全文件传送协议。可以为传输文件提供一种安全的加密方法。sftp 与 ftp 有着几乎一样的语法和功能。SFTP 为 SSH的一部分,是一种传输档案至 Blogger 伺服器的安全方式。其实在SSH软件包中,已经包含了一个叫作SFTP(Secure File Transfer Protocol)的安全文件传输子系统,SFTP本身没有单独的守护进程,它必须使用sshd守护进程(端口号默认是22)来完成相应的连接操作,所以从某种意义上来说,SFTP并不像一个服务器程序,而更像是一个客户端程序。SFTP同样是使用加密传输认证信息和传输的数据,所以,使用SFTP是非常安全的。但是,由于这种传输方式使用了加密/解密技术,所以传输效率比普通的FTP要低得多,如果您对网络安全性要求更高时,可以使用SFTP代替FTP。
本工具基于对JSch - Java Secure Channel进行的封装,linux服务器不用安装任何插件和软件
使用指南:
首先是jar包
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.42</version>
</dependency>
Sftp协议工具类-v0.1
package com.noryar.filesystem.utils;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.Vector;
import org.apache.commons.lang.StringUtils;
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;
/**
* 文件工具类.
* @author Leon Lee
*/
public class SftpUtil {
/**
* 文件路径前缀. /ddit-remote
*/
private static final String PRE_FIX = "/test-noryar";
/**
* 获取sftp协议连接.
* @param host 主机名
* @param port 端口
* @param username 用户名
* @param password 密码
* @return 连接对象
* @throws JSchException 异常
*/
public static ChannelSftp getSftpConnect(final String host, final int port, final String username,
final String password) throws JSchException {
ChannelSftp sftp = null;
JSch jsch = new JSch();
jsch.getSession(username, host, port);
Session 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;
return sftp;
}
/**
* 下载文件-sftp协议.
* @param downloadFile 下载的文件
* @param saveFile 存在本地的路径
* @param sftp sftp连接
* @return 文件
* @throws Exception 异常
*/
public static File download(final String downloadFile, final String saveFile, final ChannelSftp sftp)
throws Exception {
FileOutputStream os = null;
File file = new File(saveFile);
try {
if (!file.exists()) {
File parentFile = file.getParentFile();
if (!parentFile.exists()) {
parentFile.mkdirs();
}
file.createNewFile();
}
os = new FileOutputStream(file);
List<String> list = formatPath(downloadFile);
sftp.get(list.get(0) + list.get(1), os);
} catch (Exception e) {
exit(sftp);
throw e;
} finally {
os.close();
}
return file;
}
/**
* 下载文件-sftp协议.
* @param downloadFile 下载的文件
* @param saveFile 存在本地的路径
* @param sftp sftp连接
* @return 文件 byte[]
* @throws Exception 异常
*/
public static byte[] downloadAsByte(final String downloadFile, final ChannelSftp sftp) throws Exception {
ByteArrayOutputStream os = new ByteArrayOutputStream();
try {
List<String> list = formatPath(downloadFile);
sftp.get(list.get(0) + list.get(1), os);
} catch (Exception e) {
exit(sftp);
throw e;
} finally {
os.close();
}
return os.toByteArray();
}
/**
* 删除文件-sftp协议.
* @param deleteFile 要删除的文件
* @param sftp sftp连接
* @throws Exception 异常
*/
public static void rmFile(final String deleteFile, final ChannelSftp sftp) throws Exception {
try {
sftp.rm(deleteFile);
} catch (Exception e) {
exit(sftp);
throw e;
}
}
/**
* 删除文件夹-sftp协议.
* @param deleteFile 文件夹路径
* @param sftp sftp连接
* @throws Exception 异常
*/
public static void rmDir(final String pathString, final ChannelSftp sftp)