maven配置
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.55</version>
</dependency>
Download File
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 javagists.com
*
*/
public class DownloadFileSFTP {
public static void main(String[] args) throws Exception {
JSch jsch = new JSch();
Session session = null;
try {
session = jsch.getSession("admin", "127.0.0.1", 22);
session.setConfig("StrictHostKeyChecking", "no");
session.setPassword("pass");
session.connect();
Channel channel = session.openChannel("sftp");
channel.connect();
ChannelSftp sftpChannel = (ChannelSftp) channel;
sftpChannel.get("/tmpremote/testDownload.txt", "/tmplocal/testDownload.txt");
sftpChannel.exit();
session.disconnect();
} catch (JSchException e) {
e.printStackTrace();
} catch (SftpException e) {
e.printStackTrace();
}
}
}
Upload File
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 javagists.com
*
*/
public class UploadFileSFTP {
public static void main(String[] args) throws Exception {
JSch jsch = new JSch();
Session session = null;
try {
session = jsch.getSession("admin", "127.0.0.1", 22);
session.setConfig("StrictHostKeyChecking", "no");
session.setPassword("pass");
session.connect();
Channel channel = session.openChannel("sftp");
channel.connect();
ChannelSftp sftpChannel = (ChannelSftp) channel;
sftpChannel.put("/tmplocal/testUpload.txt", "/tmpremote/testUpload.txt");
sftpChannel.exit();
session.disconnect();
} catch (JSchException e) {
e.printStackTrace();
} catch (SftpException e) {
e.printStackTrace();
}
}
}
参考文献:
How to download and Upload a file through SFTP using java.: https://www.javagists.com/how-to-download-and-upload-a-file-through-sftp-using-java
Transferring a File Through SFTP in Java: https://www.baeldung.com/java-file-sftp