Home Java Javaftp FTP Server: List all files name



FTP Server: List all files name
Posted on: December 10, 2012 at 12:00 AM
This tutorial represents how to list the entire files name on FTP server using java.

FTP Server: List all files name 

This tutorial represents how to list all the files name on FTP server using java.

List all Files name:

We can list all the files name with the help of FTPClient class. FTPClient class provides different methods to operate the FTP server operations. There is no direct method to list only files of the ftp server. So first call method client.listFiles() to list all the files and directory and then check, it is file or not by using isFile() FTPFile method. Now you can display name of file by calling method file.getName().

Example : This example represents how to get list of files name.

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");

			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 on Ftp Server : ");
			for (FTPFile file : files) {
				if (file.isFile()) {
					System.out.println("File name : " + 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 on Ftp Server : 
File name : ApacheServer.shtml
File name : FtpDownloadFile.shtml
File name : FtpFileTransfer.shtml
File name : FtpTest.txt
File name : newFtp.txt

Related Tags for FTP Server: List all files name:


More Tutorials from this section

Ask Questions?    Discuss: FTP Server: List all files name  

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.