Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Javaftp Get all file size on FTP Server

Related Tutorials


 
 

Share on Google+Share on Google+

Get all file size on FTP Server

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

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 FtpFileSize {
	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 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");
					System.out.println();
				}
			}

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

If you enjoyed this post then why not add us on Google+? Add us to your Circles



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

Related Tutorials

Discuss: Get all file size on FTP Server  

Post your Comment


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