【问题标题】:FTPClient connection timeout/failed while trying to upload file尝试上传文件时 FTPClient 连接超时/失败
【发布时间】:2016-09-02 17:26:12
【问题描述】:

我正在尝试为 upload/download 的文件实现 FTPClient。

我没有收到任何错误,但文件没有上传。 我将 FTP 服务器配置为指向 F:\ drive,并允许 ftp_user 使用 777。

当我想显示文件夹内容时也会超时。

我的代码:

    public static void main(String[] args) 
        {       
            FTPFunctions ftpobj = new FTPFunctions("xxx.xxx.x.xxx", 21, "ftp_user", "Xxxxxxxxx");
            ftpobj.uploadFTPFile("F:\\FILES\\upload\\FTP_Test", "FTP_Test", "/FILES/FileStorage/");
ftpobj.listFTPFiles("/FILES/FileStorage/", "");
            ftpobj.disconnect();       
        }
       public FTPFunctions(String host, int port, String username, String password) throws Exception{

            ftp = new FTPClient();
            ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
            int reply;
            ftp.connect(host, port);
            System.out.println("FTP URL is:"+ftp.getDefaultPort());
            reply = ftp.getReplyCode();
            if (!FTPReply.isPositiveCompletion(reply)) {
                ftp.disconnect();
                throw new Exception("Exception in connecting to FTP Server");
            }
            ftp.login(username, password);
            ftp.setFileType(FTP.BINARY_FILE_TYPE);
            ftp.enterLocalPassiveMode();
        }

        public void uploadFTPFile(String localFileFullName, String fileName, String hostDir) throws Exception {
            try {
                InputStream input = new FileInputStream(new File(localFileFullName));
                this.ftp.storeFile(hostDir + fileName, input);
            }
            catch(Exception e)
        }

输出:

220 Microsoft FTP Service
FTP URL is:21
USER ftp_user
331 Password required for ftp_user.
PASS Xxxxxxxxx
230 User ftp_user logged in.
TYPE I
200 Type set to I.
PASV
227 Entering Passive Mode (xxx,xxx,x,xxx,208,106).
java.net.ConnectException: Connection timed out: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at org.apache.commons.net.ftp.FTPClient._openDataConnection_(FTPClient.java:924)
at org.apache.commons.net.ftp.FTPClient._storeFile(FTPClient.java:657)
at org.apache.commons.net.ftp.FTPClient.__storeFile(FTPClient.java:643)
at org.apache.commons.net.ftp.FTPClient.storeFile(FTPClient.java:2033)
at mil.dla.daps.ftp.FTPFunctions.uploadFTPFile(FTPFunctions.java:60)
at mil.dla.daps.ftp.FTPFunctions.main(FTPFunctions.java:121)

【问题讨论】:

    标签: java ftp ftp-client


    【解决方案1】:

    uploadFTPFile 方法可能存在异常。尝试将方法更改为

        public void uploadFTPFile(String localFileFullName, String fileName, String hostDir) throws Exception {
            try {
                InputStream input = new FileInputStream(new File(localFileFullName));
                this.ftp.storeFile(hostDir + fileName, input);
            }
            catch(Exception e) {
                 e.printStrackTrace();
            }
        }
    

    并查看它是否打印任何异常。

    编辑 1

    也许 ftp 服务器不支持被动模式?尝试删除

    ftp.enterLocalPassiveMode();
    

    在登录后直接添加以下行

    for (String file : ftp.listFiles("/")) {
        System.out.println("File: " + file);
    }
    

    【讨论】:

    • 添加 for 循环使 FTP 挂起:150 打开 ASCII 模式数据连接。我不得不杀了它。我真的不知道为什么这会使 ftp 旋转。删除passiveMode允许我上传文件顺便说一句,但程序卡在 150 Opening BINARY mode data connection for /FILES/FileStorage/FTP_Test"
    • 这对我来说像是防火墙/网络问题。您的 Windows 防火墙可能会阻止 jvm 连接到未知端口。请参阅此处了解如何更改防火墙设置dummies.com/computers/operating-systems/windows-7/… 您也可以尝试使用不同的 ftp 客户端以确保 ftp 服务器正常工作。
    猜你喜欢
    • 2020-02-11
    • 2013-04-07
    • 2015-05-26
    • 2019-11-29
    • 1970-01-01
    • 2012-06-14
    • 1970-01-01
    • 1970-01-01
    • 2012-12-12
    相关资源
    最近更新 更多