1

I am trying to connect to a FTP site and upload/download a file. I was able to connect via browser and FileZilla. So I know for sure that it works. But when I try to access it via my java program, I get this exception. Please any help will be appreciated.
My Java Class (constructor)

public TriFtpClient() {
    tri = new FTPClient();
    String host = InkAndTonerProperties.getSingleton().getProperty(
            "ftpDomain");
    int reply;
    try {
        tri.addProtocolCommandListener(new PrintCommandListener(
                new PrintWriter(System.out), true));
        tri.connect(host, 21);
        log.debug("default port = " + tri.getDefaultPort());
        reply = tri.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply)) {
            tri.disconnect();
            log.error("Exception in connecting to FTP Server");
        }
        tri.login(
                InkAndTonerProperties.getSingleton().getProperty(
                        "ftpUserId"), InkAndTonerProperties.getSingleton()
                        .getProperty("ftpPassword"));
        tri.enterLocalPassiveMode();
    } catch (IOException e) {
        if (tri.isConnected()) {
            log.error("it is connected.");
            try {
                tri.disconnect();
            } catch (IOException f) {
                // do nothing
            }
        }
        log.error("Failed to connect and login", e);
    }
}

// method for upload
// method for download

stack trace

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)
1
  • My code fails when it reaches tr.connect(). it was unable to establish a connection. Aug 16, 2013 at 15:42

1 Answer 1

1

Enable debug log for FTPClient and check if the timeout is occurring while entering to passive mode. Most of the time the connection timeout will occur while entering passive mode.

If the connection timeout is at that time check IP in the log below:

Entering Passive Mode (xxx,xxx,xxx,xxx,xxx,xxx)

If the first block is starting with 10 or 172 or 192, then the server is asking to connect using the private IP when entering to passive mode. You will have to change the passive address in the server side to use public IP address

3
  • the timeout occurs when creating the connection, but not when entering passive mode. I am not able to get there yet. Aug 16, 2013 at 17:32
  • Then the issue will be something else. You can check the following. Check if you are trying connect using a Domain name or IP (Usually in this case it throws Unknon host exception. Not a timeout). If using Domain Name, change to IP and try. Check if there is any proxy configured in your browser.
    – Prem
    Aug 16, 2013 at 17:47
  • Thanks Prem for the input, I am having a firewall issue here. Still trying to figure it out Aug 22, 2013 at 13:33

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.