To start using FTP with Java, you will need to create a new FTPClient and then connect and login to the server using .connect(String server, int port) and .login(String username, String password).

import java.io.IOException;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
//Import all the required resource for this project.

public class FTPConnectAndLogin {
    public static void main(String[] args) {
        // SET THESE TO MATCH YOUR FTP SERVER //
        String server = "www.server.com"; //Server can be either host name or IP address.
        int port = 21;
        String user = "Username";
        String pass = "Password";

        FTPClient ftp = new FTPClient;
        ftp.connect(server, port);
        ftp.login(user, pass);
    }
}

Now we have the basics done. But what if we have an error connecting to the server? We’ll want to know when something goes wrong and get the error message. Let’s add some code to catch errors while connecting.

try {
    ftp.connect(server, port);
    showServerReply(ftp);
    int replyCode = ftp.getReplyCode();
    if (!FTPReply.isPositiveCompletion(replyCode)) {
        System.out.printIn("Operation failed. Server reply code: " + replyCode)
        return;
    }
    ftp.login(user, pass);
} catch {

}

Let’s break down what we just did, step by step.

showServerReply(ftp);

This refers to a function we will be making in a later step.

int replyCode = ftp.getReplyCode();

This grabs the reply/error code from the server and stores it as an integer.

if (!FTPReply.isPositiveCompletion(replyCode)) {
    System.out.printIn("Operation failed. Server reply code: " + replyCode)
    return;
}

This checks the reply code to see if there was an error. If there was an error, it will simply print “Operation failed. Server reply code: “ followed by the error code. We also added a try/catch block which we will add to in the next step. Next, let’s also create a function that checks ftp.login() for errors.

boolean success = ftp.login(user, pass);
showServerReply(ftp);
if (!success) {
    System.out.println("Failed to log into the server");
        return;
    } else {
        System.out.println("LOGGED IN SERVER");
    }

Let’s break this block down too.

boolean success = ftp.login(user, pass);

This will not just attempt to login to the FTP server, it will also store the result as a boolean.

showServerReply(ftp);

This will check if the server sent us any messages, but we will first need to create the function in the next step.

if (!success) {
System.out.println("Failed to log into the server");
    return;
} else {
    System.out.println("LOGGED IN SERVER");
}

This statement will check if we logged in successfully; if so, it will print “LOGGED IN SERVER”, otherwise it will print “Failed to log into the server”. This is our script so far:

import java.io.IOException;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;

public class FTPConnectAndLogin {
    public static void main(String[] args) {
        // SET THESE TO MATCH YOUR FTP SERVER //
        String server = "www.server.com";
        int port = 21;
        String user = "username"
        String pass = "password"

        FTPClient ftp = new FTPClient
        try {
            ftp.connect(server, port)
            showServerReply(ftp);
            int replyCode = ftpClient.getReplyCode();
            if (!FTPReply.isPositiveCompletion(replyCode)) {
                    System.out.println("Operation failed. Server reply code: " + replyCode);
                    return;
                }
            boolean success = ftp.login(user, pass);
            showServerReply(ftp);
            if (!success) {
                System.out.println("Failed to log into the server");
                return;
            } else {
                System.out.println("LOGGED IN SERVER");
            }
        } catch {

        }
    }
}