简体   繁体   中英

ftp Connection timed out exception

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

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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM