2010-12-15 49 views
7

我正在嘗試使用apache-commons net FTP lib來從FTP服務器獲取get。如果目錄中只有一個文件,代碼工作正常,但第二次調用retrieveFileStream()時總是返回null。有什麼想法嗎?我寫了下面的示例代碼來演示我的問題。apache-commons ftp檢索多個文件

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

回答

17

Doh!失蹤魔術是:

completePendingCommand() 
+0

謝謝!這實際上幫助我與我的問題 – Ascalonian 2011-05-18 22:59:45

+0

同樣在這裏,謝謝! – Gevorg 2011-08-21 01:06:58

+0

我剛剛過去了一個小時左右,試圖找出這一個 - 謝謝! – kbbucks 2013-05-23 20:39:06