Although the Apache Commons Net API does not provide a direct method for getting size of a file on FTP server, there are two indirect methods which we can employ to query size of the remote file:

    • Call method mlistFile(String path) of the FTPClient class which returns a FTPFile object, and invoke getSize() method on this returned object to get actual file size in bytes. Or:
    • Send SIZE command to the FTP server using the method sendCommand(String command, String argument) and check file size by looking at the server’s reply string (return value of getReplyString() method).
Following are examples for the two methods above.

Using mlistFile() method:

FTPClient ftpClient = new FTPClient();
// code to connect and login....

String filePath = "/photos/mywedding.jpg";
FTPFile file = ftpClient.mlistFile(filePath);
long size = file.getSize();
System.out.println("File size = " + size);
The output would be:

File size = 2755863

 

Sending SIZE command:

FTPClient ftpClient = new FTPClient();
// code to connect and login....

String filePath = "/photos/mywedding.jpg";
ftpClient.sendCommand("SIZE", filePath);
String reply = ftpClient.getReplyString();
System.out.println("Reply for SIZE command: " + reply);
The output would be:

Reply for size command: 213 2755863

Note that the reply string contains a FTP reply code (which is 213 – file status) then followed by the file size (separated by a space).

To demonstrate, we created a small program that connects to a FTP server and employs the two mentioned methods to retrieve file size. Here’s the code:

package net.codejava.ftp;

import java.io.IOException;

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

/**
 * This program demonstrates how to get size of a particular file on a
 * FTP server, using Apache Commons Net API.
 * @author www.codejava.net
 *
 */
public class FTPFileSize {

	public static void main(String[] args) {
		String server = "www.yourserver.com";
		int port = 21;
		String user = "your_username";
		String pass = "your_password";

		FTPClient ftpClient = new FTPClient();
		try {
			ftpClient.connect(server, port);
			ftpClient.login(user, pass);

			String filePath = "/photos/mywedding.jpg";

			FTPFile file = ftpClient.mlistFile(filePath);
			long size = file.getSize();
			System.out.println("File size = " + size);

			ftpClient.sendCommand("SIZE", filePath);
			String reply = ftpClient.getReplyString();
			System.out.println("Reply for size command: " + reply);

			ftpClient.logout();
			ftpClient.disconnect();
		} catch (IOException ex) {
			ex.printStackTrace();
		} finally {
			if (ftpClient.isConnected()) {
				try {
					ftpClient.logout();
					ftpClient.disconnect();
				} catch (IOException ex) {
					ex.printStackTrace();
				}
			}
		}
	}
}


To test the program, remember to replace the connection settings with yours.

 

Related Java FTP Tutorials:

 

Other Java FTP Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.



Attachments:
Download this file (FTPFileSize.java)FTPFileSize.java[demo program]1 kB

Add comment

   


Comments 

#3Vuong2021-01-22 01:52
Hi Nam,
I'll try to update FTP version but can you instruct me how to catch the exception an parse text to get the size value?
Many thanks!
Quote
#2Nam2021-01-22 00:55
Hi Vuong,
I think the FTP version on the server is different than the FTP library version. So try to update the library version.
If it still not working, consider to catch that exception an parse the text to get the size value.
Quote
#1Vuong2021-01-21 23:37
Hi, I got an error message "org.apache.commons.net.MalformedServerReplyException: Invalid server reply (MLST): '250-modify=20210122053405;perm=adfrw;size=296;type=file;unique=FC01UBE40D;UNIX.group=1003;UNIX.mode=0644;UNIX.owner=1003; /EU.txt'"

It means server doesn't support MLST, right?

But the error message included "size" so How to filter only size on that ?

Thanks.
Quote