apache commons net ftpClient 425 425无法打开被动连接:无法分配请求的地址

时间:2013-05-16 17:06:55

标签: apache ftp ftp-client

我正在使用FTP客户端Apache commons net,当我尝试从服务器下载许多文件时收到此消息。

227 Entering Passive Mode (192,168,6,101,13,102)
RETR /mnt/hda/data/2013_05_15_20_50.edy
150 Opening BINARY mode data connection for '/mnt/hda/data/2013_05_15_20_50.edy' (15877 bytes).
226 Transfer complete.

TYPE I
200 Type set to I.
PASV
425 Can't open passive connection: Cannot assign requested address.

第一个文件没有问题但是对于下一个文件我得到“425无法打开被动连接:无法分配请求的地址”错误。

我注意到使用filezilla时,当我尝试下载时,连接会丢失,但filezilla会自动重新连接。

有办法检查当前连接的状态吗?我使用下一个代码:

/**
 * 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(30000); // set timeout to 5 minutes

    /* 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 (FTPFile ftpFile : ftpFiles) {

        // Check if FTPFile is a regular file           
        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.
            ftpClient.setControlKeepAliveTimeout(30000); // set timeout to 5 minutes

            //download Files 
            ftpClient.retrieveFile(
                    DefaultValuesGenerator.LINPAC_ENC_DIRPATH + filename,
                    fos
                    );


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

1 个答案:

答案 0 :(得分:0)

(在问题编辑中由OP回答。转换为社区维基答案。请参阅Question with no answers, but issue solved in the comments (or extended in chat)

OP写道:

  

这不是一个非常优雅的解决方案,但它对我有用。我注意到这是下载文件之间的连接问题我在425错误消息的情况下重新连接并尝试重试下载文件(因此i = i-2;)

/**
 * 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();
    }
}