This article provides Java code examples that demonstrate how to connect and login to a FTP server, using Apache Commons Net library. Make sure you have jar file commons-net-VERSION.jar available in your project’s classpath.

The class FTPClient (org.apache.commons.net.ftp.FTPClient) provides necessary APIs to work with a server via FTP protocol. To connect to a server, use this method: 

void connect(String server, int port)

Where server can be either host name or IP address, and port is a number (FTP protocol is using port number 21).

After connected, use this method to login: 

boolean login(String username, String password)

The login() method returns true if login successfully, false if not.

It’s advisable to check server’s reply code after each call of void method such as the connect() method above, for example:

int replyCode = ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(replyCode)) {
    // oops! The operation was not successful
    // for some reasons, the server refuses or
    // rejects requested operation
    return;
}

After each method call, the server may return back some messages. The following method displays server’s messages in the standard output:

private static void showServerReply(FTPClient ftpClient) {
    String[] replies = ftpClient.getReplyStrings();
    if (replies != null && replies.length > 0) {
        for (String aReply : replies) {
            System.out.println("SERVER: " + aReply);
        }
    }
}

The following code is for an example program:

import java.io.IOException;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
public class FTPConnectAndLoginDemo {
    private static void showServerReply(FTPClient ftpClient) {
        String[] replies = ftpClient.getReplyStrings();
        if (replies != null && replies.length > 0) {
            for (String aReply : replies) {
                System.out.println("SERVER: " + aReply);
            }
        }
    }
    public static void main(String[] args) {
        String server = "www.yourserver.net";
        int port = 21;
        String user = "username";
        String pass = "password";
        FTPClient ftpClient = new FTPClient();
        try {
            ftpClient.connect(server, port);
            showServerReply(ftpClient);
            int replyCode = ftpClient.getReplyCode();
            if (!FTPReply.isPositiveCompletion(replyCode)) {
                System.out.println("Operation failed. Server reply code: " + replyCode);
                return;
            }
            boolean success = ftpClient.login(user, pass);
            showServerReply(ftpClient);
            if (!success) {
                System.out.println("Could not login to the server");
                return;
            } else {
                System.out.println("LOGGED IN SERVER");
            }
        } catch (IOException ex) {
            System.out.println("Oops! Something wrong happened");
            ex.printStackTrace();
        }
    }
}

Compile and run the example program:

compile and run FTP login and connect program

 

Related Java FTP Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.



Attachments:
Download this file (FTPConnectAndLoginDemo.java)FTPConnectAndLoginDemo.java[Java source code]1 kB

Add comment

   


Comments 

#19SootminPick2017-09-11 06:42
This tutorial is:
  • Nice
  • Cool
  • Watchable all day 'till puke comes out
Quote
#18Anshul2016-04-10 06:26
FTP response 421 received. Server closed connection.

it shows this error !
Please help me sir !
Quote
#17Ramkrishna Joshi2015-12-15 05:13
[quote name="Subha Mukherjee"]Sir, i used the code for FTP via lan to a particular Ip.
but it shows error.
Code:::
String server = "192.168.1.101";
int port = 21;
String user = "user";
String pass = "infotech";

Error:::


Have you found out any solution for this
Quote
#16Soumi2015-09-11 01:24
Hi Sir While I am compiling the code using server "www.google.com,Port 21 it is showing the error message"Oops ...Something Wrong happened".
Could you suggest me how to get out from this.
Quote
#15Chema2015-06-24 09:31
Thanks!
Quote