Share on Google+Share on Google+

Get all file size on FTP Server

In this section we are going to describe how to get size of FTP server files using java.

Get all file size on FTP Server

In this section we are going to describe how to get size of FTP server files using java.

Get File size :

FTPClient class does not provide any direct method to find the size of file. So we will use another way to list the file size. FTPFile class provide you method getSize() to get the size of the current file.

FTPFile class handles files stored on FTP server. It shows you different information about the files.

long getSize() : This method returns size of file in bytes. Its return type is long.

Example :

In this example, we are listing all files and their size. For that first list all files and store it in FTPFile array. Now iterate and check if it is file by usinf method isFile() then get its name using method getName()(as file.getName()) and get its size using method getSize() (as file.getSize()).

Here is complete source code -



class FtpFileSize {
	public static void main(String[] args) throws IOException {
		FTPClient client = new FTPClient();
		boolean result;
		try {
			result = client.login("admin", "admin");

			if (result == true) {
				System.out.println("User successfully logged in.");
			} else {
				System.out.println("Login failed!");

			FTPFile[] files = client.listFiles();
			System.out.println("Files and their size on Ftp Server : ");
			for (FTPFile file : files) {
				if (file.isFile()) {
					System.out.print("File name : " + file.getName());
					System.out.print("\t size : " + file.getSize()+" Bytes");

		} catch (FTPConnectionClosedException e) {
		} finally {
			try {
			} catch (FTPConnectionClosedException e) {

Output :

User successfully logged in.
Files and their size on Ftp Server : 
File name : ApacheServer.shtml	 size : 1823 Bytes
File name : FtpDownloadFile.shtml	 size : 2623 Bytes
File name : FtpFileTransfer.shtml	 size : 3312 Bytes
File name : FtpTest.txt	 size : 462 Bytes
File name : newFtp.txt	 size : 21 Bytes


Posted on: December 10, 2012 If you enjoyed this post then why not add us on Google+? Add us to your Circles

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.

Discuss: Get all file size on FTP Server  

Post your Comment

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