javaftpapache-commons-netconnectexception

Cannot connect FTP server in java


I'm trying to develop a FTP client in java using Apache Commons Net but even if my code seems to be good I can't connect to any server and I get :

java.net.ConnectException: connect: Address is invalid on local machine, or port is not valid on remote machine
    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 java.net.Socket.connect(Unknown Source)
    at java.net.Socket.<init>(Unknown Source)
    at java.net.Socket.<init>(Unknown Source)
    at org.apache.commons.net.ftp.FTPHTTPClient.connect(FTPHTTPClient.java:131)
    at org.apache.commons.net.SocketClient.connect(SocketClient.java:296)
    at awax.meteosat.test.FTPClientTest.connect(FTPClientTest.java:52)
    at awax.meteosat.test.FTPClientTest.main(FTPClientTest.java:146)
client disconnected
Exception in thread "main" java.lang.IllegalStateException: Client disconnected
    at awax.meteosat.test.FTPClientTest.login(FTPClientTest.java:100)
    at awax.meteosat.test.FTPClientTest.main(FTPClientTest.java:147)

I tried to connect to this server : ftp://ics.ftptest.org/, which works perfectly. I tried to disconnect my firewall and that leeds to the same results.

Here is my code, I hope that it will obvious for you guys haha :

package awax.meteosat.test;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.SocketException;
import java.net.URL;

import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPHTTPClient;
import org.apache.commons.net.ftp.FTPReply;

public class FTPClientTest {

    private final String host;
    private final int port;
    private final String username;
    private final String password;

    private org.apache.commons.net.ftp.FTPClient client;

    /**
     * Instanciate FTP server.
     * 
     * @param host
     *            FTP server address.
     * @param username
     *            FTP username.
     * @param password
     *            FTP password.
     */
    public FTPClientTest (final String address, final String username, final String password) {
        URL url = null;
        try {
            url = new URL(address);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        this.host = url != null ? url.getHost() : null;
        this.port = 0;
        this.username = username;
        this.password = password;
    }

    public void connect () {
        if (this.client == null) {
            this.client = new FTPHTTPClient(this.host, this.port, this.username, this.password);
            try {
                // Connecting client
                if (this.port > 0) {
                    this.client.connect(this.host, this.port);
                } else {
                    this.client.connect(this.host);
                }

                // Check connection
                int reply = this.client.getReplyCode();
                if (!FTPReply.isPositiveCompletion(reply)) {
                    disconnect();
                } else {
                    setFtpOptions();
                }
            } catch (SocketException e) {
                e.printStackTrace();
                disconnect();
            } catch (IOException e) {
                e.printStackTrace();
                disconnect();
            }
        } else {
            System.err.println("client is null");
        }
    }

    public void disconnect () {
        if (this.client != null) {
            if (this.client.isConnected()) {
                try {
                    this.client.disconnect();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } else {
                System.err.println("client disconnected");
            }
        } else {
            System.err.println("client is null");
        }
    }

    public boolean login () {
        if (isConnected()) {
            try {
                return this.client.login(this.username, this.password);
            } catch (IOException e) {
                e.printStackTrace();
                logout();
                return false;
            }
        } else {
            throw new IllegalStateException("Client disconnected");
        }
    }

    public boolean logout () {
        if (isConnected()) {
            try {
                return this.client.logout();
            } catch (IOException e) {
                e.printStackTrace();
                return false;
            }
        } else {
            throw new IllegalStateException("Client disconnected");
        }
    }

    public FTPFile[] listDirectories () {
        if (isConnected()) {
            try {
                return this.client.listDirectories();
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }
        } else {
            throw new IllegalStateException("Client disconnected");
        }
    }

    public boolean isConnected () {
        if (this.client != null) {
            return this.client.isConnected();
        } else {
            return false;
        }
    }

    private void setFtpOptions () {
        if (isConnected()) {
            this.client.enterLocalPassiveMode();
        }
    }

    public static void main (String args[]) {
        FTPClientTest ftp = new FTPClientTest("ftp://ics.ftptest.org/", "", "");
        ftp.connect();
        ftp.login();
        for (FTPFile f : ftp.listDirectories()) {
            System.err.println(f);
        }
        ftp.logout();
        ftp.disconnect();
    }
}

Solution

  • Try using FTPClient instead of FTPHTTPClient(which according to it's documentation is experimental HTTP proxying of FTP connections)...

    public void connect () {
        if (this.client == null) {
            this.client = new FTPClient();
    

    should work just fine