Java apache commons ftp检索多个文件

Java apache commons ftp检索多个文件,java,ftp,apache-commons-net,Java,Ftp,Apache Commons Net,我正在尝试使用ApacheCommonsNetFTP库从FTP服务器获取数据。如果目录中只有1个文件,则代码可以正常工作,但在第二次调用retrieveFileStream()时总是返回null。有什么想法吗?我已经编写了以下示例代码来演示我的问题 public static void main(String[] args) throws Exception { String strLine; FTPClient client = null; try{

我正在尝试使用ApacheCommonsNetFTP库从FTP服务器获取数据。如果目录中只有1个文件,则代码可以正常工作,但在第二次调用retrieveFileStream()时总是返回null。有什么想法吗?我已经编写了以下示例代码来演示我的问题

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
    }
  }

啊!缺少的魔法是:

completePendingCommand()

谢谢这实际上帮助了我解决了我的问题。我刚刚花了一个小时左右的时间来想这个问题——谢谢!!