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) http://kodehelp.com 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 http://kodehelp.com
 *
 */
public class SFTPinJava {

	
	/**
	 * @param args
	 */
	@SuppressWarnings("unchecked")
	public static void main(String[] args) {
		String SFTPHOST = "10.20.30.40";
		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);
			session.setPassword(SFTPPASS);
			java.util.Properties config = new java.util.Properties();
			config.put("StrictHostKeyChecking", "no");
			session.setConfig(config);
			session.connect();
			channel = session.openChannel("sftp");
			channel.connect();
			channelSftp = (ChannelSftp)channel;
			channelSftp.cd(SFTPWORKINGDIR);
			Vector filelist = channelSftp.ls(SFTPWORKINGDIR);
			for(int i=0; i<filelist.size();i++){
				System.out.println(filelist.get(i).toString());
			}
			
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
}
  • 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?
      Thanks!

      • Vishwa Vijay

        ChannelSftp c = getSftpChanel();

        @SuppressWarnings(“unchecked”)

        Vector flLst = c.ls(baseFolder);

        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()){

        flNmLst.add(ent.getFilename());

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

        }

        }

  • ayush sanghvi

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