How to get list of Files from SFTP Server in Java?

To get list of files from SFTP server you have to use JSCH API. For more details on how to use JSCH API please refer to by previous post Java program for Downloading File from SFTP server.

Below Java code show how to get the list of files from SFTP server.

package com.kodehelp.sftp;

import java.nio.channels.Channel;
import java.util.Vector;

import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;

 * @author
public class SFTPinJava {

     * @param args
    public static void main(String[] args) {
        String SFTPHOST = "";
        int SFTPPORT = 22;
        String SFTPUSER = "USERNAME";
        String SFTPPASS = "PASSWORD";
        String SFTPWORKINGDIR = "/export/home/kodehelp/";

        Session session = null;
        Channel channel = null;
        ChannelSftp channelSftp = null;

        try {
            JSch jsch = new JSch();
            session = jsch.getSession(SFTPUSER, SFTPHOST, SFTPPORT);
            java.util.Properties config = new java.util.Properties();
            config.put("StrictHostKeyChecking", "no");
            channel = session.openChannel("sftp");
            channelSftp = (ChannelSftp) channel;
            Vector filelist =;
            for (int i = 0; i < filelist.size(); i++) {

        } catch (Exception ex) {



  1. BramCorvus says:

    The code isn’t complete… can you update it please?

  2. Fandy Akhmad says:

    Hello , how i can get each information. May be i want to get just filename without attribut.
    And i want to break down thi information in each variabel.

    -rw-r–r– 1 medifile medifile 124 Feb 19 09:39 .bashrc
    -rw-r–r– 1 medifile medifile 33 Feb 19 09:39 .bash_logout

    Thank you…

  3. ayush sanghvi says:

    This code gives me AUTH failed error can anyone help me on this?

  4. Pat Ellery says:

    com.jcraft.jsch.JSchException: session is down

Please Post Your Comments & Reviews

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.