【问题标题】:FTP Apache Commons stalls at retrieveFileFTP Apache Commons 在retrieveFile 处停止
【发布时间】:2013-05-08 23:03:13
【问题描述】:

我创建了一个小测试程序供以后使用,它只是连接到服务器并检索文件,如果看起来像这样:

import org.apache.commons.net.ftp.*;
import java.io.*;
import java.net.SocketException;


public class Test {
     private final String pass = [I removed this password];
        FTPClient ftp;
        File file = new File("download.txt");
        private int reply;
        FileOutputStream dfile;

        public void ftp() {
            try {
                ftp = new FTPClient();
                ftp.connect("ftp.bevilacqua.me");
                ftp.login([i removed this username] ,[I removed this password]);

                reply = ftp.getReplyCode();

                if(FTPReply.isPositiveCompletion(reply)) {
                    System.out.println("Connected Success");
                } else {
                    System.out.println("Connection unsuccessful");
                    ftp.disconnect();
                }

                if(file.exists()) {
                    System.out.println("File already exists");
                } 

                dfile = new FileOutputStream(file);


                ftp.retrieveFile("untitled.txt", dfile); //Untitled does exist in directory '/'
                System.out.println("Success... maybe");


            } catch(SocketException ex) {
                ex.printStackTrace();
            } catch(IOException ex) {
                ex.printStackTrace();
            }


        }

    public static void main(String[] args) {
        Test main = new Test();
        main.ftp();
        try {
            main.ftp.disconnect();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

程序打印连接成功并且文件已经存在,因此它必须是检索文件。程序不会崩溃,也不会抛出异常。

【问题讨论】:

    标签: java apache ftp apache-commons


    【解决方案1】:

    我会先试试这些:

    1) 被动模式(这指示客户端进行所有连接,而不是服务器) ftp.enterLocalPassiveMode();

    2) 数据超时(这应该会阻止无限期挂起,但如果您将值设置得太短,那么您可以摆脱早期的慢速连接)它很难模拟和测试 - 试试吧。 ftp.setDataTimeout(10000);//十秒

    最终有一种方法可以将 protocolMessageHandler 传递到底层套接字,这样您就应该能够记录客户端和服务器之间的聊天记录。虽然我从来没有这样做过,但我想它很有可能揭示真正的问题。

    【讨论】:

      猜你喜欢
      • 2011-10-02
      • 1970-01-01
      • 2015-10-11
      • 2013-06-17
      • 2014-08-09
      • 2013-02-24
      • 2011-05-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多