In this example we will see how we can use JSch library to login to SFTP server and download files.
First, add the following dependency to your pom.xml
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.54</version> <!-- or latest version -->
</dependency>
JSch apis are pretty simple. First you create a session and open a channel then you can use one of the many function such as CD, LS, PUT, GET to change directory, list content, upload file or download respectively.
Create Session:
JSch jsch = new JSch();
Session session = jsch.getSession("demo", "test.rebex.net", 22);
session.setPassword("password");
session.connect();
Create Channel:
ChannelSftp channel = (ChannelSftp) session.openChannel("sftp");
channel.connect();
Change folder:
channelSftp.cd("/a/folder");
List content of a folder:
Vector<ChannelSftp.LsEntry> entries = channelSftp.ls(folder);
Download file:
channelSftp.get(String fileNameInFtp, String destinationFile);
Upload File:
channelSftp. put(String src, String dst) //default is overwrite
channelSftp. put(String src, String dst, int mode)
Upload Modes:
public static final int OVERWRITE=0;
public static final int RESUME=1;
public static final int APPEND=2;
A Complete Example Code to download files from FTP:
In this example, we are using a publicly available ftp server as described in https://test.rebex.net/
import com.jcraft.jsch.*;
import java.io.File;
import java.util.*;
public class JschDownload {
public static void main(String[] args) {
Session session = null;
ChannelSftp channel = null;
try {
JSch jsch = new JSch();
session = jsch.getSession("demo", "test.rebex.net", 22);
session.setPassword("password");
//to prevent following exception for sftp
//com.jcraft.jsch.JSchException: UnknownHostKey: test.rebex.net. RSA key fingerprint is ..
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
System.out.println("session connected");
//various channels are supported eg: shell, x11,
channel = (ChannelSftp) session.openChannel("sftp");
channel.connect();
System.out.println("channel connected");
downloadFromFolder(channel, "/");
downloadFromFolder(channel, "/pub/example/");
//in order to download all files including sub-folders/sub-sub-folder, we should iterate recursively
System.out.println("File Uploaded to FTP Server Successfully.");
} catch (Exception e) {
e.printStackTrace();
} finally {
if (channel != null) {
channel.disconnect();
}
if (channel != null) {
session.disconnect();
}
}
}
static void downloadFromFolder(ChannelSftp channelSftp, String folder) throws SftpException {
Vector<ChannelSftp.LsEntry> entries = channelSftp.ls(folder);
new File("download").mkdir();
//download all files (except the ., .. and folders) from given folder
for (ChannelSftp.LsEntry en : entries) {
if (en.getFilename().equals(".") || en.getFilename().equals("..") || en.getAttrs().isDir()) {
continue;
}
System.out.println("Downloading " + (folder + en.getFilename()) + " ----> " + "download" + File.separator + en.getFilename());
channelSftp.get(folder + en.getFilename(), "download" + File.separator + en.getFilename());
}
}
}
No comments :
Post a Comment
Your Comment and Question will help to make this blog better...