0

我正在使用 FTP Client 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();
    }
}
4

1 回答 1

0

(由 OP 在问题编辑中回答。转换为社区 wiki 答案。请参阅没有答案的问题,但问题在评论中解决(或在聊天中扩展)

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();
    }
}
于 2015-01-30T20:02:19.960 回答