0

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)

1 Answer 1

2

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);
}
Sign up to request clarification or add additional context in comments.

2 Comments

adding that for loop made FTP hang: 150 Opening ASCII mode data connection. I had to kill it. I really don't know why that made ftp spin. Removing passiveMode allowed me to upload file btw, but the program is stuck on 150 Opening BINARY mode data connection for /FILES/FileStorage/FTP_Test"
This looks like a firewall/network issue to me. Your windows firewall might be blocking the jvm from making connections to unknown ports. See here on how to change the firewall settings dummies.com/computers/operating-systems/windows-7/… You could also try to use a different ftp client to ensure that the ftp server is working porperly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.