Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

apache-commons ftp retrieve multiple files

I'm attempting to use apache-commons net FTP lib to do a get from a FTP server. The code works fine if there's only 1 file in the directory, but always returns null the second time I call retrieveFileStream(). Any thoughts? I've written the following example code to demonstrate my problem.

public static void main(String[] args) throws Exception
  {
    String strLine;
    FTPClient client = null;

    try{
      client = new FTPClient();
      client.connect("localhost", 21);
      client.enterLocalPassiveMode();
      client.login("ftptester", "letmein");

      client.changeWorkingDirectory("remote");

      FTPFile[] ftpFiles = client.listFiles();          
      if (ftpFiles != null && ftpFiles.length > 0) {
        for (FTPFile file : ftpFiles) {
          if (!file.isFile()) {
            continue;
          }

          InputStream fin = client.retrieveFileStream(filepath);
          if (fin == null) {
            System.out.println("could not retrieve file: " + filepath);
            continue;
          }

          byte[] data = readBytes(fin);  // helper method not shown, just processes the input stream
          fin.close();
          fin = null;

          System.out.println("data: " + new String(data));          
        }
      }
    }
    finally {
      ...  // cleanup code
    }
  }
like image 595
fmpdmb Avatar asked Dec 15 '10 17:12

fmpdmb


1 Answers

Doh! Missing magic was:

completePendingCommand()
like image 165
fmpdmb Avatar answered Oct 18 '22 02:10

fmpdmb