Angelina
Angelina

Reputation: 2265

FTPClient connection timeout/failed while trying to upload file

I am trying to implement FTPClient for upload/download of files.

I don't get any errors, but file is not uploaded. I configured my FTP server to point to F:\ drive and allowed 777 for ftp_user.

It times out when I want to display folder contents, also.

My code:

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

Output:

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)

Upvotes: 0

Views: 6142

Answers (1)

Guenther
Guenther

Reputation: 2045

There might be an exception in the uploadFTPFile method. Try changing the method to

    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();
        }
    }

and see if it prints any exceptions.

EDIT 1

Maybe the ftp server doesn't support passive mode? Try removing

ftp.enterLocalPassiveMode();

add the following lines directly after the login

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

Upvotes: 2

Related Questions