第一步:pom.xml文件中导入相关依赖
<!-- SFTP上传 -->
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.45</version>
</dependency>
第二步:编写工具类
import com.jcraft.jsch.*;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
/**
* sftp工具类,包含以下功能:
* 获取sftp链接
* 关闭sftp链接
* 下载文件
* 上传文件
* 删除文件
* 查找文件
* 更多功能自行拓展
*/
public class SftpUtil {
/**
* 获取一个sftp链接
* @param host sftp服务器ip
* @param port 端口
* @param username 用户名
* @param password 密码
* @return 返回ChannelSftp
* @throws Exception 获取通道时的异常
*/
public static ChannelSftp getSftpChannel(String host, Integer port, String username, String password) throws Exception{
Session session;
Channel channel = null;
JSch jSch = new JSch();
try {
session = jSch.getSession(username, host, port);
session.setPassword(password);
// 配置链接的属性
Properties properties = new Properties();
properties.setProperty("StrictHostKeyChecking","no");
session.setConfig(properties);
// 进行sftp链接
session.connect();
// 获取通信通道
channel = session.openChannel("sftp");
channel.connect();
} catch (JSchException e) {
e.printStackTrace();
throw e;
}
return (ChannelSftp)channel;
}
/**
* 上传文件
* @param channelSftp sftp通道
* @param localFile 本地文件(文件地址+文件名+文件后缀)
* @param remoteFile 远程文件(文件地址+文件名+文件后缀)
*/
public static void upload(ChannelSftp channelSftp, String localFile, String remoteFile) throws Exception{
InputStream inputStream = null;
try {
inputStream = new FileInputStream(localFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
channelSftp.put(inputStream, remoteFile);
inputStream.close();
}
/**
* 从sftp服务器上下载文件
* @param channelSftp sftp通道
* @param remoteFile 远程文件
* @param localFile 本地文件
*/
public static void download(ChannelSftp channelSftp, String remoteFile, String localFile) throws Exception{
OutputStream outputStream = null;
try {
outputStream = new FileOutputStream(localFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
channelSftp.get(remoteFile, outputStream);
outputStream.close();
}
/**
* 删除文件
* @param channelSftp sftp通道
* @param remoteFile 远程文件
*/
public static void deleteFile(ChannelSftp channelSftp, String remoteFile) throws Exception{
try {
channelSftp.rm(remoteFile);
} catch (SftpException e) {
e.printStackTrace();
throw e;
}
}
/**
* 1.根据指定目录获取文件夹下的文件列表
* 2.根据文件绝对地址获取单个文件对象列表
* @param channelSftp
* @param directory
* @return
*/
@SuppressWarnings("unchecked")
public static List<Object> viewDirectory(ChannelSftp channelSftp,String directory){
List<Object> list = new ArrayList<Object>();
try {
list = channelSftp.ls(directory);
} catch (SftpException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
/**
* 关闭sftp链接
* @param channelSftp sftp通道
* @throws Exception 关闭session的异常
*/
public static void closeSession(ChannelSftp channelSftp) throws Exception{
if(channelSftp == null){
return;
}
Session session = null;
try {
session = channelSftp.getSession();
} catch (JSchException e) {
e.printStackTrace();
throw e;
}finally {
if(session != null){
session.disconnect();
}
}
}
}
注意事项:
使用时要先获取sftp连接,然后使用对应的文件操作方法,操作完成后及时关闭sftp连接