import java.io.*;
import java.util.*;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.ChannelSftp.LsEntry;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
public class SftpClientUtil {
ChannelSftp sftp = null;
private String host = "";
private int port = 0;
private String username = "";
private String password = "";
public SftpClientUtil(String host, int port, String username,String password) {
this.host = host;
this.port = port;
this.username = username;
this.password = password;
}
public void connect() throws Exception {
JSch jsch = new JSch();
Session sshSession = jsch.getSession(this.username, this.host);
sshSession.setPassword(password);
Properties sshConfig = new Properties();
sshConfig.put("StrictHostKeyChecking", "no");
sshSession.setConfig(sshConfig);
sshSession.connect();
Channel channel = sshSession.openChannel("sftp");
channel.connect();
this.sftp = (ChannelSftp) channel;
}
public void disconnect() throws Exception {
if (this.sftp != null) {
if (this.sftp.isConnected()) {
this.sftp.disconnect();
} else if (this.sftp.isClosed()) {
}
}
}
public void upload(String directory, String uploadFile) throws Exception {
this.sftp.cd(directory);
File file = new File(uploadFile);
this.sftp.put(new FileInputStream(file), file.getName());
}
public void uploadByDirectory(String directory) throws Exception {
String uploadFile = "";
List<String> uploadFileList = this.listFiles(directory);
Iterator<String> it = uploadFileList.iterator();
while (it.hasNext()) {
uploadFile = it.next().toString();
this.upload(directory, uploadFile);
}
}
public void download(String directory, String downloadFile, String saveDirectory) throws Exception {
String saveFile = saveDirectory + "//" + downloadFile;
this.sftp.cd(directory);
File file = new File(saveFile);
this.sftp.get(downloadFile, new FileOutputStream(file));
}
public void downloadByDirectory(String directory, String saveDirectory) throws Exception {
String downloadFile = "";
List<String> downloadFileList = this.listFiles(directory);
Iterator<String> it = downloadFileList.iterator();
while (it.hasNext()) {
downloadFile = it.next().toString();
if (downloadFile.toString().indexOf(".") < 0) {
continue;
}
this.download(directory, downloadFile, saveDirectory);
}
}
public void delete(String directory, String deleteFile) throws Exception {
this.sftp.cd(directory);
this.sftp.rm(deleteFile);
}
@SuppressWarnings("unchecked")
public List<String> listFiles(String directory) throws Exception {
Vector fileList;
List<String> fileNameList = new ArrayList<String>();
fileList = this.sftp.ls(directory);
Iterator it = fileList.iterator();
while (it.hasNext()) {
String fileName = ((LsEntry) it.next()).getFilename();
if (".".equals(fileName) || "..".equals(fileName)) {
continue;
}
fileNameList.add(fileName);
}
return fileNameList;
}
public void rename(String directory, String oldFileNm, String newFileNm) throws Exception {
this.sftp.cd(directory);
this.sftp.rename(oldFileNm, newFileNm);
}
public void cd(String directory) throws Exception {
this.sftp.cd(directory);
}
public InputStream get(String directory) throws Exception {
InputStream streatm = this.sftp.get(directory);
return streatm;
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了