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

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 'org.apache.commons.net.ftp.FTPClient' 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.

view plainprint?

  1.  
  2.   
  3.  commons-net 
  4.  commons-net 
  5.  2.0 
  6.   
  7.   
  8.  commons-io 
  9.  commons-io 
  10.  2.4 
  11.   
  12.  
 <dependencies>
  <dependency>
   <groupId>commons-net</groupId>
   <artifactId>commons-net</artifactId>
   <version>2.0</version>
  </dependency>
  <dependency>
   <groupId>commons-io</groupId>
   <artifactId>commons-io</artifactId>
   <version>2.4</version>
  </dependency>

 </dependencies>


Create connection with FTP server in Java - Example Code


view plainprint?

  1. package com.beingjavaguys.testftp; 
  2. import java.io.IOException; 
  3. import java.net.SocketException; 
  4. import org.apache.commons.net.ftp.FTPClient; 
  5. publicclass FtpConnection { 
  6.  publicstaticvoid main(String args[]) { 
  7.  // get an ftpClient object
  8.  FTPClient ftpClient = new FTPClient(); 
  9.  try { 
  10.  // pass directory path on server to connect
  11.  ftpClient.connect("nagesh12.5gbfree.com"); 
  12.  // pass username and password, returned true if authentication is
  13.  // successful
  14.  boolean login = ftpClient.login("nagesh12", "password"); 
  15.  if (login) { 
  16.  System.out.println("Connection established..."); 
  17.  System.out.println("Status: "+ftpClient.getStatus()); 
  18.  // logout the user, returned true if logout successfully
  19.  boolean logout = ftpClient.logout(); 
  20.  if (logout) { 
  21.  System.out.println("Connection close..."); 
  22.  } 
  23.  } else { 
  24.  System.out.println("Connection fail..."); 
  25.  } 
  26.  } catch (SocketException e) { 
  27.  e.printStackTrace(); 
  28.  } catch (IOException e) { 
  29.  e.printStackTrace(); 
  30.  } finally { 
  31.  try { 
  32.  ftpClient.disconnect(); 
  33.  } catch (IOException e) { 
  34.  e.printStackTrace(); 
  35.  } 
  36.  } 
  37.  }