尝试上传文件时,FTPClient连接超时

时间:2019-10-14 18:04:18

标签: java ftp ftp-client

我正在使用org.apache.commons.net.ftp.FTPClient将文件上传到Windows FTP服务器。

我在将文件上传到FTP服务器时遇到了问题(还有没有内容的文件)。我得到Connection timed out (Connection timed out)

奇怪的是,我能够创建文件夹。

这是我的代码:

public FtpClient getFtpClient() throws ConnectException {
    FTPClient ftpClient = new FTPClient();
    try {

        ftpClient.connect("localhost", 21);
        ftpClient.enterLocalPassiveMode();

        ftpClient.login("username", "password");

        final int loginResponse = ftpClient.getReplyCode();

        if (!FTPReply.isPositiveCompletion(loginResponse)) {
            ftpClient.disconnect();
        }

        ftpClient.setFileType(BINARY_FILE_TYPE);

        ftpClient.setRemoteVerificationEnabled(false);

    } catch (final IOException e) {}

    return ftpClient;
}


private void connectAndSend(String message) throws FileTransferException {
    try ((final FTPClient ftpClient = getFtpClient())) {
        // code to create dir if they do not exist goes here
        final ByteArrayInputStream inputStream = new ByteArrayInputStream(message.getBytes());
      ftpClient.storeFile("c:\\upload\\folderA\\subfolderB\\2019-10-14-20.11.54.111.fileName.dat", inputStream);
    } catch (IOException e) {}
}

为什么我可以创建文件夹,但是当我尝试上传文件时却得到Connection timed out (Connection timed out)

无论有没有ftpClient.enterLocalPassiveMode();,我都尝试过,没有运气。

我也尝试过使用FTP客户端将文件放在与Java程序相同的机器上的FTP客户端上的FTP服务器中。

我能够使用FileZilla(位于单独的计算机上)将相同的文件上传到FTP服务器。

Java程序位于EC2实例中运行的docker映像中。 FileZilla在Windows计算机上运行。

更新:

这是Java程序中试图将文件放入FTP服务器的日志。

2019-10-15 13:19:57.017 INFO 1 --- [ask-scheduler-3] u.c.h.g.a.service.ftp.FtpSender : File built for dest D:\FTP\FolderOne\FolderTwo\FolderThree
PASV
530 Please login with USER and PASS.
USER *******
331 Password required
PASS *******
230 User logged in.
2019-10-15 13:19:57.048 INFO 1 --- [ask-scheduler-3] u.c.h.g.a.service.ftp.FtpClient : Logged into FTP Server
TYPE I
200 Type set to I.
CWD D:\
550 The parameter is incorrect.
2019-10-15 13:19:57.050 INFO 1 --- [ask-scheduler-3] u.c.h.g.a.service.ftp.FtpSender : Folder D:\ does not exist. Creating folder D:\
MKD D:\
550 The parameter is incorrect.
CWD FTP
250 CWD command successful.
2019-10-15 13:19:57.052 INFO 1 --- [ask-scheduler-3] u.c.h.g.a.service.ftp.FtpSender : Folder FTP exist. Do nothing
CWD FolderOne
250 CWD command successful.
2019-10-15 13:19:57.053 INFO 1 --- [ask-scheduler-3] u.c.h.g.a.service.ftp.FtpSender : Folder FolderOne exist. Do nothing
CWD FolderTwo
250 CWD command successful.
2019-10-15 13:19:57.054 INFO 1 --- [ask-scheduler-3] u.c.h.g.a.service.ftp.FtpSender : Folder FolderTwo exist. Do nothing
CWD FolderThree
250 CWD command successful.
2019-10-15 13:19:57.055 INFO 1 --- [ask-scheduler-3] u.c.h.g.a.service.ftp.FtpSender : Folder FolderThree exist. Do nothing
2019-10-15 13:19:57.058 INFO 1 --- [ask-scheduler-3] u.c.h.g.a.service.ftp.FtpSender : uploading to pre.env.co.uk:21 D:\FTP\FolderOne\FolderTwo\FolderThree\2019-10-15-14.19.57.169751.fileName.dat as username
PASV
227 Entering Passive Mode (10,128,5,21,213,99).
2019-10-15 13:22:07.746 ERROR 1 --- [ask-scheduler-3] u.c.h.g.a.service.ftp.FtpSender : FAILED uploading to pre.env.co.uk:21
||AI021|20191014|181439|8160394620251766|||81|4074|DATA|ADAT|||DATA|1111111|111111|11111111111|||11|1111|DATA|ADAT|.|DATA||||DATA|94|816035||||802||15|004|||||N|N|DATA||||||||||||||||||704721919||||DATA|0|||DATA|||||||||DATA||||DATA|11|11111||||111||111|1111|||||1|1|1111||||||||||||||||||704721919||||DATA|0|||DATA||||||||
as username.
{}
java.net.ConnectException: Connection timed out (Connection timed out)

以下是与Java程序在同一台计算机上运行的FTP客户端的日志

root@955a11ba6126:/usr/src/ftp# ftp -d -p dmx01.pre.examplecloud.co.uk
Connected to dmx01.pre.examplecloud.co.uk.
220 Microsoft FTP Service
ftp: setsockopt: Bad file descriptor
Name (dmx01.pre.examplecloud.co.uk:root): dmx01-ftp
---> USER dmx01-ftp
331 Password required
Password:
---> PASS XXXX
230 User logged in.
---> SYST
215 Windows_NT
Remote system type is Windows_NT.
ftp> cd FTP
---> CWD FTP
250 CWD command successful.
ftp> put cat
local: cat remote: cat
ftp: setsockopt (ignored): Permission denied
---> PASV
227 Entering Passive Mode (10,128,5,21,219,253).
ftp: connect: Connection timed out
ftp> put cat
local: cat remote: cat
ftp: setsockopt (ignored): Permission denied
---> PASV
421 Service not available, remote server has closed connection
Passive mode refused.
ftp> 

为什么Java程序和FTP客户端无法将文件放入FTP服务器。 Java程序和FTP客户端在同一台计算机上运行。

1 个答案:

答案 0 :(得分:0)

FTPClient.storeFile的第一个参数是:

  

remote-提供远程文件的名称。

我不认为"c:\\upload\\folderA\\subfolderB\\2019-10-14-20.11.54.111.fileName.dat"是您的FTP服务器的有效远程路径。使用在使用FTP客户端登录时看到的路径。