本文介绍了apache-commons ftp检索多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用apache-commons net FTP lib从FTP服务器获取一个get。如果目录中只有一个文件,代码工作正常,但是第二次调用retrieveFileStream()时总是返回null。有什么想法吗?我写了下面的示例代码来演示我的问题。

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


推荐答案

Doh!缺少的魔术是:

Doh! Missing magic was:

completePendingCommand()

这篇关于apache-commons ftp检索多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 14:28