0

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();
    }
}
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 に答える