Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Get list of files from FTP Server in Java - Using Commons Net API

In today's discussion we will came across 'How to get list of files from 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 get list of files from 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)  Use listFiles() to get an array of file information from the currently working directory.
5)  Call logout() method of API on ftpClient to logout from connected ftp server. It returns 'true' if logout is successful and false otherwise.
6)  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.

<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>


Get list of files from FTP Server in Java - Example Code



package com.beingjavaguys.testftp;

import java.io.IOException;
import java.net.SocketException;

import org.apache.commons.io.FileUtils;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;

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

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

try {
// pass directory path on server to connect
ftpClient.connect("nagesh12.5gbfree.com");

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

if (login) {
System.out.println("Connection established...");

// get all files from server and store them in an array of
// FTPFiles
FTPFile[] files = ftpClient.listFiles();

for (FTPFile file : files) {
if (file.getType() == FTPFile.FILE_TYPE) {
System.out.println("File Name: "
+ file.getName()
+ " File Size: "
+ FileUtils.byteCountToDisplaySize(file
.getSize()));
}
}

// 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) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}


Output : Here is expected output, connection started, files name and size printed and connection ends.



Here we are done with 'Get list of files from FTP Server in Java - Using Commons Net API'. In our upcoming blogs we will see more about Java Programming and other opensource technologies.








Thanks for reading !
Being Java Guys Team


RELATED POSTS



Create connection with FTP server in Java


Upload files to FTP Server in Java


Download files from FTP Server in Java


Delete files from FTP Server in Java


Create and Delete a directory on FTP Server in Java





This post first appeared on Java, Struts 2, Spring, Hibernate, Solr, Mahout An, please read the originial post: here

Share the post

Get list of files from FTP Server in Java - Using Commons Net API

×

Subscribe to Java, Struts 2, Spring, Hibernate, Solr, Mahout An

Get updates delivered right to your inbox!

Thank you for your subscription

×