【问题标题】:apache-commons ftp retrieve multiple filesapache-commons ftp 检索多个文件
【发布时间】:2011-05-26 01:50:42
【问题描述】:

我正在尝试使用 apache-commons net FTP lib 从 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
    }
  }

【问题讨论】:

    标签: java ftp apache-commons-net


    【解决方案1】:

    哇!缺少的魔法是:

    completePendingCommand()
    

    【讨论】:

    • 谢谢!这实际上帮助我解决了我的问题
    • 我在最后一个小时左右试图弄清楚这个 - 谢谢!!
    猜你喜欢
    • 2020-08-26
    • 2013-05-30
    • 2012-12-11
    • 2013-08-04
    • 2011-10-02
    • 2012-01-25
    • 2021-10-13
    • 1970-01-01
    • 2012-03-20
    相关资源
    最近更新 更多