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

DeeKay April 29, 2011 at 3:25 pm · How To

To get a 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.

 * Created on Nov, 2004 Copyright(c) All Rights Reserved. 
package com.kodehelp.sftp;

import java.util.Vector;

import com.jcraft.jsch.Channel;
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 SFTPWORKINGDIR = "/export/home/kodehelp/";
		Session 	session 	= null;
		Channel 	channel 	= null;
		ChannelSftp channelSftp = null;
			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){
  • BramCorvus

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

    • Thanks for pointing out this. I have fixed this. Please check.

  • Fandy Akhmad

    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…

    • Hector

      Yes, I have the same problem, how can I get only the filename?

      • Vishwa Vijay

        ChannelSftp c = getSftpChanel();


        Vector flLst =;

        final int i = flLst.size();

        List flNmLst = new ArrayList();

        for(int j=0;j<i;j++){

        LsEntry ent = flLst.get(j);

        SftpATTRS attr = ent.getAttrs();

        if(!attr.isDir() && !attr.isLink()){


        System.out.println(" File : "+ent.getFilename());



  • ayush sanghvi

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