apache ftpClient 425 Cannot open passive connection

  • 需要进入passive模式:this.ftpClient.enterLocalPassiveMode();
/**
 * Download encrypted and configuration files.
 * 
 * @throws SocketException
 * @throws IOException
 */
public void downloadDataFiles(String destDir) throws SocketException,
        IOException {

    String filename;
    //  log.debug("ftpServer: "  + ftpServer);
    this.ftpClient.connect(ftpServer);
    this.ftpClient.login(ftpUser, ftpPass);
    ftpClient.setControlKeepAliveTimeout(300); // set timeout to 5 minutes
    ftpClient.setDataTimeout(300);

    /* CHECK NEXT 4 Methods (included the commented) 
    *  they were very useful for me!
    *  and icreases the buffer apparently solve the problem!!
    */
    //  ftpClient.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out), true));
    //log.debug("Buffer Size:" + ftpClient.getBufferSize());
    this.ftpClient.setBufferSize(1024 * 1024);
    //log.debug("Buffer Size:" + ftpClient.getBufferSize());


    /*  
     *  get Files to download
     */
    this.ftpClient.enterLocalPassiveMode();
    this.ftpClient.setAutodetectUTF8(true);
    this.ftpClient.enterLocalPassiveMode();
    FTPFile[] ftpFiles = ftpClient
            .listFiles(DefaultValuesGenerator.LINPAC_ENC_DIRPATH);

    /*
     * Download files
     */

    for (int i=0; i<ftpFiles.length;i++) {
        log.debug("INICIO i value: "+ i);
        FTPFile ftpFile =  ftpFiles[i];
        // Check if FTPFile is a regular file
        log.debug("INICIO i value: "+ i + " - file: " + ftpFile.getName());
        if (ftpFile.getType() == FTPFile.FILE_TYPE) {
            try{

            filename = ftpFile.getName();

            // Download file from FTP server and save
            fos = new FileOutputStream(destDir + filename);

            this.ftpClient.setFileType(FTP.BINARY_FILE_TYPE);

            //TODO: check if connection still alive.
            //log.debug("passive: "+ftpClient.getPassiveHost() + ":"+ ftpClient.getPassivePort()); 
                            //               
            //download Files 
            ftpClient.retrieveFile(
                    DefaultValuesGenerator.LINPAC_ENC_DIRPATH + filename,
                    fos
                    );


            log.debug("RUN i value: "+ i);
            if(ftpClient.getReplyCode() == 425){
                log.debug("REPLY CODE: "+ftpClient.getReplyCode());
                log.debug("ARCHIVO: "+ filename);

                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }


                this.ftpClient.disconnect();
            }

            }catch(FTPConnectionClosedException e){
                log.debug("FTPConnectionClosedException");
            }catch(CopyStreamException e){
                log.debug("CopyStreamException ");
            }catch(IOException e){
                log.debug("IOException - reconecting to server");

                this.ftpClient.connect(ftpServer);
                this.ftpClient.login(ftpUser, ftpPass);
                this.ftpClient.enterLocalPassiveMode();

                i=i-2; //para poder volver a intentar descargar el archivo
                log.debug("Exception i value: "+ i);

            }finally{
                fos.flush();
                fos.close();                }
        }
    }
    if (fos != null) {
        fos.close();
    }
}

StackOverFlow Link