Open In App

How to Download Files From SFTP Server in Java?

Last Updated : 21 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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 {
  
        // create a instance of sftpConnector
        SFTPConnector sftpConnector = new SFTPConnector();
  
        // getting sftpClient by calling method of sftpConnector
        SFTPClient sftpClient = sftpConnector.connectToSftpServer();
  
        try {
            String remoteDir = "/test_user/demo";
            // list all the files from the sftp server for specific directory.
            List<RemoteResourceInfo> resourceInfoList = sftpClient.ls(remoteDir);
  
            // files will be stored in this directory after downloading.
            String downloading_dir = "/home/malav/Downloads/test/";
  
            for (RemoteResourceInfo file : resourceInfoList) {
                // download or get file from the sftp server.
                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 {
  
        // create a instance of sftpConnector
        SFTPConnector sftpConnector = new SFTPConnector();
  
        // getting sftpClient by calling method of sftpConnector
        SFTPClient sftpClient = sftpConnector.connectToSftpServer();
  
        try {
            String remoteDir = "/test_user/demo";
            // list all the files from the sftp server for specific directory.
            List<RemoteResourceInfo> resourceInfoList = sftpClient.ls(remoteDir);
  
            // files will be stored in this directory after downloading.
            String downloading_dir = "/home/malav/Downloads/test/";
  
            for (RemoteResourceInfo file : resourceInfoList) {
                // download or get file from the sftp server.
                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

Files downloaded from Sftp server



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads