title: How to get list of files from an SFTP server?
body: <p>I have a problem and hoping to get a solution. I also have written some code but it needs some modification.</p>
  <p>Problem:
  I have a SFTP server (for privacy purposes I will give dummy credentials) that I need to connect to.</p>
  <p>Server name: server-name<br />
  port: 22<br />
  username: username<br />
  password: password</p>
  <p>When I connect to the server, it automatically drops me in the <code>/FGV</code> directory. inside this directory are couple other folders. I need to grab a LIST of xml messages from the <code>/FGV/US/BS/</code> directory and place them in a LIST (files in the form of File). In the list, I need to have the directory of the file, file name and the file body. I was thinking of creating an object and putting this information in there and creating a List of that object.</p>
  <p>My current code creates a connection and downloads only ONE xml file. If there are two xml files, then the file in my local machine has nothing as content.</p>
  <pre><code>import java.io.BufferedInputStream;
  import java.io.BufferedOutputStream;
  import java.io.File;
  import java.io.FileOutputStream;
  import java.io.OutputStream;
  
  import com.jcraft.jsch.Channel;
  import com.jcraft.jsch.ChannelSftp;
  import com.jcraft.jsch.JSch;
  import com.jcraft.jsch.Session;
  
  public class SFTPinJava {
  
      public SFTPinJava() {
      }
  
      public static void main(String[] args) {
          String SFTPHOST = &quot;server-name&quot;;
          int SFTPPORT = 22;
          String SFTPUSER = &quot;username&quot;;
          String SFTPPASS = &quot;password&quot;;
          String SFTPWORKINGDIR = &quot;/FGV&quot;;
  
          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(&quot;StrictHostKeyChecking&quot;, &quot;no&quot;);
              session.setConfig(config);
              session.connect();
              channel = session.openChannel(&quot;sftp&quot;);
              channel.connect();
              channelSftp = (ChannelSftp) channel;
              channelSftp.cd(SFTPWORKINGDIR);
              byte[] buffer = new byte[1024];
              BufferedInputStream bis = new BufferedInputStream(
                  channelSftp.get(&quot;/FGV/US/BS/FGVCustomsEntryLoaderService.xml&quot;));
              File newFile = new File(
                  &quot;C:\\workspace\\Crap\\src\\org\\raghav\\stuff\\XML_FROM_SERVER.xml&quot;);
              OutputStream os = new FileOutputStream(newFile);
              BufferedOutputStream bos = new BufferedOutputStream(os);
              int readCount;
              //System.out.println(&quot;Getting: &quot; + theLine);
              while ((readCount = bis.read(buffer)) &gt; 0) {
                  //System.out.println(&quot;Writing: &quot;);
                  bos.write(buffer, 0, readCount);
              }
              
              while(session != null){
                  System.out.println(&quot;Killing the session&quot;);
                  session.disconnect();
                  bis.close();
                  bos.close();
                  System.exit(0);
              }
              
          } catch (Exception ex) {
              ex.printStackTrace();
          }
      }
  }
  </code></pre>
  <p>I need to change this code so that it would grab multiple files and puts them in a list of objects. that object should have the directory of the file, the file name and the body of the file.</p>