apache-commons ftp retrieve multiple files
Asked Answered
F

1

8

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
    }
  }
Finalize answered 15/12, 2010 at 17:30 Comment(0)
F
19

Doh! Missing magic was:

completePendingCommand()
Finalize answered 15/12, 2010 at 18:23 Comment(1)
I've just been the last hour or so trying to figure this one - thanks!!Reflux

© 2022 - 2024 — McMap. All rights reserved.