Java and J2EE Tutorials, Jsp and Servlet Tutorials, Spring MVC, Solr, XML, JSON Examples, Hibernate & Struts 2 Hello World projects

Thursday, 12 December 2013

Create connection with FTP server in Java - Using Commons Net API

In today's discussion we will came across 'How to establish connection with FTP server in Java'. We are using 'apache commons-net' API that holds all necessary classes to deal with FTP operations like, create connection , get list of all files on ftp , upload file to ftp , download files from ftp , create and delete a directory on ftp and delete a file on ftp.

In this particular blog we will see an example of how to connect to ftp server in java, all steps included in the process are:

1)  Get a 'FtpClient' object from '' Class.
2)  Use 'connect()'  method of API to open a connection to the FTP Server, pass ftp path or url as           parameter to connect() method.
3)  Call login() method of API on ftpClient, and pass server credentials as parameter. It returns 'true' if login is successful and false otherwise.
4)  Call logout() method of API on ftpClient to logout from connected ftp server. It returns 'true' if logout is successful and false otherwise.
5)  Call disconnect() method of API on ftpClient to end connection from connected ftp server. It returns 'true' if disconnect is successful and false otherwise.

Required Libraries to add

To use implementation of 'Commons Net' we need to add following dependency to our pom.xml. Here 'commons-net' is required dependency but 'commons-io' is optional. 'commons-io' provides a number of general purpose implementation to deal with IO.


Create connection with FTP server in Java - Example Code

package com.beingjavaguys.testftp;


public class FtpConnection {
 public static void main(String args[]) {

  // get an ftpClient object
  FTPClient ftpClient = new FTPClient();

  try {
   // pass directory path on server to connect

   // pass username and password, returned true if authentication is
   // successful
   boolean login = ftpClient.login("nagesh12", "password");

   if (login) {
    System.out.println("Connection established...");
    System.out.println("Status: "+ftpClient.getStatus());
    // logout the user, returned true if logout successfully
    boolean logout = ftpClient.logout();
    if (logout) {
     System.out.println("Connection close...");
   } else {
    System.out.println("Connection fail...");

  } catch (SocketException e) {
  } catch (IOException e) {
  } finally {
   try {
   } catch (IOException e) {

Output : Here is expected output, connection started, status string printed and connection ends.

Here we are done with 'Create connection with FTP server in Java - Using Commons Net'. In our upcoming blogs we will see more about Java Programming and other opensource technologies.

Thanks for reading !
Being Java Guys Team


  1. what are the "Required Libraries to add", what do I do here? add to what, I know it says pom.xml but no idea what that is :/

    1. this is already mentioned there , you need to add 'commons-net' and 'commons-io'

    2. ok, is the pom.xml file the thing in the commons net? if it is then I've put it there, but no difference. Keeps saying package doesn't exist and I don't know why, think it's to do with the way I've added the "commons-net-3.3" file with "apache-maven-3.2.1" but I've no idea, looked all over for some kind of instruction but can't find anything, any links?

  2. How to read file from ftp server and copy directory from one location to another location on same server?

  3. How to copy folder and files from one location to another location on same ftp server?

  4. new luckyUser();25 January 2015 at 07:01

    Thank you, Nagesh.


Like Us on Facebook

Like Us On Google+


Skype: neel4soft