Home Java Javaftp FTP Server : List Files and Directories



FTP Server : List Files and Directories
Posted on: December 6, 2012 at 12:00 AM
In this tutorial we will discuss how to list files and directories on FTP server using java

FTP Server : List Files and Directories

In this tutorial we will discuss how to list files and directories on FTP server using java.

List Files and Directories :

By using FTP Server we can transfer files from one computer to another computer through network and internet. You must be valid user for using FTP server. Before applying any operation on FTP server first we need to connect to the server by using connect(hostname) method of FTPClient class next input valid user name and password to login by calling method login("username","password");

client.connect("localhost");
result = client.login("admin", "admin");

listFiles() : This method of FTPClient is used to list all files and directories of the current FTP server directory. This method returns array of FTPFile object which list the files and directories.

FTPFile[] files = client.listFiles();

FTPFile class supply many methods which is used to handle file and directories like getName(), getSize(), isDirectory(), isFile(), getTimestamp().

Example : In this example we are listing all the files and directories of FTP server and displaying the same.

import java.io.IOException;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPConnectionClosedException;
import org.apache.commons.net.ftp.FTPFile;

class FTPListFiles {
	public static void main(String[] args) throws IOException {
		FTPClient client = new FTPClient();
		boolean result;
		try {
			client.connect("localhost");
			result = client.login("admin", "admin");
			client.changeWorkingDirectory("c:\ftp");
			if (result == true) {
				System.out.println("User successfully logged in.");
			} else {
				System.out.println("Login failed!");
				return;
			}
			FTPFile[] files = client.listFiles();
			System.out.println("Files and directories on Ftp Server directory : ");
			for (FTPFile file : files) {
				System.out.println(file.getName());
			}

		} catch (FTPConnectionClosedException e) {
			System.out.println(e);
		} finally {
			try {
				client.disconnect();
			} catch (FTPConnectionClosedException e) {
				System.out.println(e);
			}
		}
	}
}

Output :

User successfully logged in.
Files and directories on Ftp Server directory : 
.
..
ApacheServer.shtml
filetest
FtpConnect.shtml
FtpTopic.txt
FtpTutorial.txt

Related Tags for FTP Server : List Files and Directories:


More Tutorials from this section

Ask Questions?    Discuss: FTP Server : List Files and Directories  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.