2010-12-15 27 views
7

Próbuję użyć apache-commons netto FTP lib, aby uzyskać od serwera FTP. Kod działa poprawnie, jeśli w katalogu znajduje się tylko jeden plik, ale zawsze zwraca wartość null przy drugim wywołaniu funkcji retrieveFileStream(). jakieś pomysły? Napisałem poniższy kod przykładowy, aby pokazać mój problem.ftp apache-commons wielu plików

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

Odpowiedz

17

Doh! Brakująca magia:

completePendingCommand() 
+0

Dzięki! To faktycznie pomogło mi z moim problemem – Ascalonian

+0

To samo, dzięki! – Gevorg

+0

Po prostu byłem ostatnia godzina, starając się to wymyślić - dzięki! – kbbucks

Powiązane problemy