2009-02-03 8 views
8

Estoy usando org.apache.commons.net.ftp.FTPClient y veo un comportamiento que es, bueno ... desconcertante.Problema con la clase FTPClient en java

El siguiente método intenta pasar por una lista FTPFile, leerlos y luego hacer algo con los contenidos. Eso está funcionando. Lo que no es (muy) de trabajo es que el objeto FtpClient hace lo siguiente ...

1) Properly retrieves and stores the FIRST file in the list 
2) List item evaluates to NULL for x number of successive iterations of the loop (x varies on successive attempts 
3) manages to retrieve exactly 1 more file in the list 
4) reports that it is null for exactly 1 more file in the list 
5) hangs indefinitely, reporting no further activity. 

public static String mergeXMLFiles(List<FTPFile> files, String rootElementNodeName, FTPClient ftp){ 
     String ret = null; 
     String fileAsString = null; 
     //InputStream inStream; 
     int c; 

     if(files == null || rootElementNodeName == null) 
      return null; 
     try { 
      System.out.println("GETTING " + files.size() + " files"); 
      for (FTPFile file : files) { 
       fileAsString = ""; 
       InputStream inStream = ftp.retrieveFileStream(file.getName()); 

       if(inStream == null){ 
        System.out.println("FtpUtil.mergeXMLFiles() couldn't initialize inStream for file:" + file.getName()); 

        continue;//THIS IS THE PART THAT I SEE FOR files [1 - arbitrary number (usually around 20)] and then 1 more time for [x + 2] after [x + 1] passes successfully. 
       } 
       while((c = inStream.read()) != -1){ 

        fileAsString += Character.valueOf((char)c); 
       } 
       inStream.close(); 


       System.out.println("FILE:" + file.getName() + "\n" + fileAsString); 
      } 


     } catch (Exception e) { 
      System.out.println("FtpUtil.mergeXMLFiles() failed:" + e); 
     } 
     return ret; 
    } 

alguien ha visto algo como esto? Soy nuevo en FTPClient, ¿estoy haciendo algo mal con eso?

+0

Podría editar el post así que en lugar de usar la vista de código para los elementos de lista que en lugar de utilizar los códigos cortos previstos elementos de la lista? Lo hace más legible de esa manera :) – Kezzer

Respuesta

14

De acuerdo con la API para FTPClient.retrieveFileStream(), el método devuelve null cuando no se puede abrir la conexión de datos, en cuyo caso se debe comprobar el código de respuesta (por ejemplo getReplyCode(), getReplyString(), getReplyStrings()) para ver qué ha fallado. Además, se supone que debes finalizar las transferencias de archivos llamando al completePendingCommand() y verificando que la transferencia fue exitosa.

+2

completePendingComand() !!!!! Eres un caballero Y un erudito Estaba comprobando el código de respuesta (en el método anterior donde hago todos los mensajes basura de conexión) y todo funcionaba bien. Pasé por alto la documentación que especificaba la importancia de completarPendingCommand. Muchas gracias. –

+0

Gracias por esta respuesta Zach Scrivena! Estaba atascado con este problema durante mucho tiempo :) – krishnang

2

Funciona bien cuando agrego después del comando "recuperar":

 int response = client.getReply(); 
     if (response != FTPReply.CLOSING_DATA_CONNECTION){ 
      //TODO 
     } 
Cuestiones relacionadas