JSch库来实现SFTP(Secure File Transfer Protocol)的文件下载和上传
import com.jcraft.jsch.*;
public class SftpExample {
public static void main(String[] args) {
String host = "your_sftp_host";
int port = 22;
String username = "your_username";
String password = "your_password";
// 文件下载
String remoteFilePath = "/remote/path/to/file.txt";
String localFilePath = "local/path/to/save/file.txt";
downloadFile(host, port, username, password, remoteFilePath, localFilePath);
// 文件上传
String localFileToUpload = "local/path/to/upload/file.txt";
String remoteDirectory = "/remote/path/to/upload/";
uploadFile(host, port, username, password, localFileToUpload, remoteDirectory);
}
public static void downloadFile(String host, int port, String username, String password, String remoteFilePath, String localFilePath) {
try {
JSch jsch = new JSch();
Session session = jsch.getSession(username, host, port);
session.setPassword(password);
session.setConfig("StrictHostKeyChecking", "no");
session.connect();
ChannelSftp channelSftp = (ChannelSftp) session.openChannel("sftp");
channelSftp.connect();
channelSftp.get(remoteFilePath, localFilePath);
channelSftp.disconnect();
session.disconnect();
System.out.println("File downloaded successfully.");
}
catch (JSchException | SftpException e) {
e.printStackTrace();
}
}
public static void uploadFile(String host, int port, String username, String password, String localFilePath, String remoteDirectory) {
try {
JSch jsch = new JSch();
Session session = jsch.getSession(username, host, port);
session.setPassword(password);
session.setConfig("StrictHostKeyChecking", "no");
session.connect();
ChannelSftp channelSftp = (ChannelSftp) session.openChannel("sftp");
channelSftp.connect();
channelSftp.put(localFilePath, remoteDirectory);
channelSftp.disconnect();
session.disconnect();
System.out.println("File uploaded successfully.");
}
catch (JSchException | SftpException e) {
e.printStackTrace();
}
}
}
import com.jcraft.jsch.*;
public class SftpExample {
public static void main(String[] args) {
String host = "your_sftp_host";
int port = 22;
String username = "your_username";
String password = "your_password";
// 文件夹下载
String remoteFolderPath = "/remote/path/to/folder";
String localFolderPath = "local/path/to/save/folder";
downloadFolder(host, port, username, password, remoteFolderPath, localFolderPath);
}
public static void downloadFolder(String host, int port, String username, String password, String remoteFolderPath, String localFolderPath) {
try {
JSch jsch = new JSch();
Session session = jsch.getSession(username, host, port);
session.setPassword(password);
session.setConfig("StrictHostKeyChecking", "no");
session.connect();
ChannelSftp channelSftp = (ChannelSftp) session.openChannel("sftp");
channelSftp.connect();
// 创建本地文件夹
java.nio.file.Path localPath = java.nio.file.Paths.get(localFolderPath);
java.nio.file.Files.createDirectories(localPath);
// 下载文件夹
channelSftp.get(remoteFolderPath + "/*", localFolderPath);
channelSftp.disconnect();
session.disconnect();
System.out.println("Folder downloaded successfully.");
}
catch (JSchException | SftpException | java.io.IOException e) {
e.printStackTrace();
}
}
}