2010-12-15 58 views
7

J'essaie d'utiliser apache-commons net FTP lib pour faire un get à partir d'un serveur FTP. Le code fonctionne correctement s'il n'y a qu'un seul fichier dans le répertoire, mais retourne toujours la seconde fois que j'appelle retrieveFileStream(). Des pensées? J'ai écrit l'exemple de code suivant pour illustrer mon problème.apache-commons ftp récupérer plusieurs fichiers

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

Répondre

17

Doh! La magie manquante était:

completePendingCommand() 
+0

Merci! Cela m'a vraiment aidé avec mon problème – Ascalonian

+0

Même ici, merci! – Gevorg

+0

Je viens d'être la dernière heure ou alors essayer de comprendre celui-ci - merci !! – kbbucks