javaftpftp-client

FTPClient connection timeout while trying to upload file


I am using org.apache.commons.net.ftp.FTPClient to upload files to a Windows FTP server.

I am having issues uploading files to the FTP server (also files with no content). I am getting Connection timed out (Connection timed out).

Strangely I am able to create folders.

Here is what my code looks like:

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) {}
}

Why is it that I am able to create the folder but when I try to upload a file I get Connection timed out (Connection timed out).

I've tried with and without ftpClient.enterLocalPassiveMode(); and no luck.

I have also tryied to put files into the FTP server using FTP client on the same machine as the java program with not luck.

I was able to upload the same file to the FTP server using FileZilla (located on a separate machine).

The Java program is in a docker image running in EC2 instance. FileZilla is running on a Windows machine.

UPDATE:

Here is the logs from the java program that's attempting to put files into FTP server.

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)

Here are the logs from FTP client running on the same machine as the Java program

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> 

Why is it that the java program and the FTP client can't put files into the FTP server. The java program and the FTP client are running on the same machine.


Solution

  • The first argument of FTPClient.storeFile is:

    remote - The name to give the remote file.

    I do not think that "c:\\upload\\folderA\\subfolderB\\2019-10-14-20.11.54.111.fileName.dat" is a valid remote path of your FTP server. Use the path you see when you login with your FTP client.