Home Java Javaftp FTP Server: List all files name
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

FTP Server: List all files name

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

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: December 10, 2012

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

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com