How do I connect to FTP server?

The File Transfer Protocol (FTP) is a standard network protocol used to transfer computer files between a client and server on a computer network. The example below shows you how to connect to an FTP server.

In this example we are using the FTPClient class of the Apache Commons Net library. To connect to the server, we need to provide the FTP server name. Login to the server can be done by calling the login() method of this class with a valid username and password. To logout we call the logout() method.

Let’s try the code snippet below:




public class FTPConnectDemo {
    public static void main(String[] args) {
        FTPClient client = new FTPClient();

        try {

            // When login success, the login method returns true.
            boolean login = client.login("demo", "password");
            if (login) {
                System.out.println("Login success...");

                // When logout success, the logout method returns true.
                boolean logout = client.logout();
                if (logout) {
                    System.out.println("Logout from FTP server...");
            } else {
                System.out.println("Login fail...");

        } catch (IOException e) {
        } finally {
            try {
                // Closes the connection to the FTP server
            } catch (IOException e) {

Maven Dependencies


Maven Central



    • You can simply use the textual representation of the IP address as the argument to the connect method. For example FTPClient.connect(""). Or you can create an InetAddress object using InetAddress.getByName("") and then pass this InetAddress as the argument to the connect method.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.