How ToJSchSFTP

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

SFTP - Secure File Transfer Protocol

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 https://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();
        }
    }
}

8 Comments

  1. 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…

    1. Hector ·

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

      1. 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());

        }

        }

  2. ayush sanghvi ·

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

    1. @patellery:disqus Without looking at the code i would not be able to help you out. Can you please share the code removing login credentials ?

Leave a Comment

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