Open In App

How to Get a List of Files From the SFTP Server in Java?

Last Updated : 21 Dec, 2022
Like Article

In this tutorial, we’ll talk about how to use Java to retrieve a list of files from an SFTP server. We will use SSHClient for creating an SFTP client. First, add a maven dependency to your project.



To connect to the SFTP server, we use SSHClient. In addition to providing host key verification, we must also supply login credentials. SFTPClient will be created by SSHClient, and we can use that object to carry out various tasks on the SFTP server. Here, we are retrieving a list of all files from a specific server directory. The process of listing files from an SFTP server essentially involves five phases.

  • Make a new SSHClient instance.
  • Sftp server connection.
  • Provide the Sftp server’s username and password.
  • Make a new SFTPClient object.
  • Specify a path or server directory to list files from the SFTP server.



import net.schmizz.sshj.SSHClient;
import net.schmizz.sshj.sftp.RemoteResourceInfo;
import net.schmizz.sshj.sftp.SFTPClient;
import net.schmizz.sshj.transport.verification.PromiscuousVerifier;
import java.util.List;
public class SftpListFiles {
    public static void main(String[] args) throws IOException {
        // create a instance of SSHClient
        SSHClient client = new SSHClient();
        // add host key verifier
        client.addHostKeyVerifier(new PromiscuousVerifier());
        // connect to the sftp server
        // authenticate by username and password.
        client.authPassword("test_user", "123");
        // get new sftpClient.
        SFTPClient sftpClient = client.newSFTPClient();
        // Give the path to the directory from which
        // you want to get a list of all the files.
        String remoteDir = "/test_user/demo";
        List<RemoteResourceInfo> resourceInfoList =;
        for (RemoteResourceInfo file : resourceInfoList) {
            System.out.printf("File name is %s", file.getName());


List files from Sftp server

List files from Sftp server

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads