How to Download Files From SFTP Server in Java?
Last Updated :
21 Dec, 2022
A network protocol called SSH, commonly referred to as Secure Shell or Secure Socket Shell, enables a secure connection between two computers across an insecure network. This tutorial will show you how to connect to a remote SFTP server using Java and the SSH client.
Host key verification must be taken into consideration before the connection is made. One or more HostKeyVerifier objects must be specified to do this. For host key verification, a database of known hostname-key pairs in the OpenSSH “known hosts” format can be imported.
Implementation
The SFTP server connection process consists of five key steps:
- Put SSHClient into existence.
- Create a host key verifier.
- Sftp server connection
- SFTP server login information should be provided.
- Create a new SFTPClient object.
File: SFTPConnector.java
Java
import net.schmizz.sshj.sftp.RemoteResourceInfo;
import net.schmizz.sshj.sftp.SFTPClient;
import java.io.IOException;
import java.util.List;
public class SFTPDownloader {
public static void main(String[] args) throws IOException {
SFTPConnector sftpConnector = new SFTPConnector();
SFTPClient sftpClient = sftpConnector.connectToSftpServer();
try {
String remoteDir = "/test_user/demo" ;
List<RemoteResourceInfo> resourceInfoList = sftpClient.ls(remoteDir);
String downloading_dir = "/home/malav/Downloads/test/" ;
for (RemoteResourceInfo file : resourceInfoList) {
sftpClient.get(file.getPath(), downloading_dir);
System.out.printf( "%s is downloaded%n" , file.getName());
}
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
sftpClient.close();
}
}
}
|
To start with,
- We will obtain the SFTPClient object we require from the SftpConnector class.
- The remote directory from which we wish to download all the data must be provided.
- Include the local location where downloaded files should be stored.
The line below will download the file and save it to the local directory (downloading dir).
sftpclient.get(file.getPath(),downloading_dir)
Sftpclient should be closed after.
File: SFTPDownloader.java
Java
import net.schmizz.sshj.sftp.RemoteResourceInfo;
import net.schmizz.sshj.sftp.SFTPClient;
import java.io.IOException;
import java.util.List;
public class SFTPDownloader {
public static void main(String[] args) throws IOException {
SFTPConnector sftpConnector = new SFTPConnector();
SFTPClient sftpClient = sftpConnector.connectToSftpServer();
try {
String remoteDir = "/test_user/demo" ;
List<RemoteResourceInfo> resourceInfoList = sftpClient.ls(remoteDir);
String downloading_dir = "/home/malav/Downloads/test/" ;
for (RemoteResourceInfo file : resourceInfoList) {
sftpClient.get(file.getPath(), downloading_dir);
System.out.printf( "%s is downloaded%n" , file.getName());
}
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
sftpClient.close();
}
}
}
|
Output:
Files downloaded from Sftp server
Share your thoughts in the comments
Please Login to comment...