0

I am working on a project which will later upload a few files to an FTP server after they are modified...I have everything but uploading the file figured out. I can successfully connect to the FTP server, but once the file goes to upload, the program hangs for a couple minutes, then it states that it timed out.

java.net.ConnectException: Connection timed out: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:579)
    at org.apache.commons.net.ftp.FTPClient._openDataConnection_(FTPClient.java:762)
    at org.apache.commons.net.ftp.FTPClient._storeFile(FTPClient.java:565)
    at org.apache.commons.net.ftp.FTPClient.__storeFile(FTPClient.java:557)
    at org.apache.commons.net.ftp.FTPClient.storeFile(FTPClient.java:1795)
    at AdvertisementCreator.main(AdvertisementCreator.java:128)

 

Here is the code I have for the FTP connection: (Keep in mind I omitted the login details)

FTPClient fClient = new FTPClient();
try {
    fClient.connect(server, port);
    showServerReply(fClient);
    int replyCode = fClient.getReplyCode();
    if (!FTPReply.isPositiveCompletion(replyCode)) {
        System.out.println("Operation failed. Server reply code: " + replyCode);
        return;
    }
    boolean success = fClient.login(user, pass);
    showServerReply(fClient);
    if (!success) {
        System.out.println("Could not login to the server");
    } else {
        System.out.println("You are now logged on!");
        loginLoop = false;
    }
    fClient.enterLocalPassiveMode();
    fClient.setFileType(FTP.BINARY_FILE_TYPE);
    File localFile = new File("files\\shared.txt");
    String remoteFile = "shared.txt";
    InputStream inputStream = new FileInputStream(localFile);
    System.out.println("Start uploading the file");
    boolean done = fClient.storeFile(remoteFile, inputStream);
    inputStream.close();
    if (done) {
        System.out.println(remoteFile+" has been uploaded successfully");
    }
} catch (IOException ex) {
    System.out.println("Oops! Something wrong happened");
    ex.printStackTrace();
} finally {
    try {
        if (fClient.isConnected()) {
            fClient.logout();
            fClient.disconnect();
            System.out.println("FTP Disconnected");
        }
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

I have never really messed with Apache Commons FTP until today. If anyone could provide some insight, I would greatly appreciate it

Edit: I forgot to mention that before adding the following line, the file transferred, but when I tried to open it on the server, it was empty.

fClient.enterLocalPassiveMode();
fClient.setFileType(FTP.BINARY_FILE_TYPE);
user207421
  • 305,947
  • 44
  • 307
  • 483
user4490680
  • 1
  • 1
  • 1
  • 2
  • You can't use passive mode unless you have opened a port in your firewall and forwarded it, and as you can't control what the passive port is you can't know where to forward it to: so, you can't use passive mode at all. So don't. NB Please format your code rationally. – user207421 Jul 06 '23 at 04:16

1 Answers1

0

This is a common conectivity problem that you'll have to debug first:

First, ensure the name of the server is correctly typed. Also ensure that you have properly specified a port (if, for some reason, it is not the well-known FTP port 21).

Then, ensure you have TCP conectivity between your local host and the remote FTP server: For example, open a command shell and try to execute "telnet <host> 21".

  • If some error happens, you CAN NOT connect to that server. It is not accessible from your location.
  • Else, if the terminal clears out, then you can exit: A connection has been succesfully made. In this case, check if you are using a proxy in your system setup (in windows, for example, it is seen on Control Panel\Internet Options\Connections\LAN). If yes, you have to set the same proxy parameters to the JVM when running it:
java -Dftp.socksProxyHost=<proxy host> -Dftp.socksProxyPort=<proxy port> -Djava.net.socks.username=<proxy username> -Djava.net.socks.password=<proxy user password> -classpath ... MainClass

Look all the networking properties in Java.

And yet another possibility is that you have some local firewall blocking this connection. Check it out and read the firewall's manual.

Little Santi
  • 8,563
  • 2
  • 18
  • 46
  • I added something to it the original post Little Santi...I tried it without entering passive mode and setting the file type before by mistake, and it transferred to the server, but the file was empty when I would try to open it. I am not sure if this affects anything. – user4490680 Jan 24 '15 at 21:51
  • So what's the situation right now? The connection-timed-out error is still arising? Or the file got to be transfered and now the received file is empty? Posting the execution traces might help. – Little Santi Jan 24 '15 at 23:21
  • My current issue is the connection time out. Here is what I am using in my code, I looked at the tutorial here http://www.codejava.net/java-se/networking/ftp/java-ftp-file-upload-tutorial-and-example – user4490680 Jan 25 '15 at 19:54
  • I have broken it down some. It appears to be an issue when entering LocalPassive Mode. When I don't enter local passive, thats when I get blank files uploaded. – user4490680 Jan 26 '15 at 02:39
  • It was an issue with the server, this has been solved. – user4490680 Jan 26 '15 at 03:22
  • @user4490680 It was *what* issue with the server? – user207421 Jul 06 '23 at 04:17