Java FTP Get size of a file on server
- Details
- Written by Nam Ha Minh
- Last Updated on 19 July 2019   |   Print Email
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:
- Determine if a directory or file exists on FTP server
- Java FTP example - Get and set file modification time
- Java FTP example - Get details of a file or directory on server
- Rename file or directory on FTP server
- Delete a file on a FTP server
Other Java FTP Tutorials:
- Connect and login to a FTP server
- Java FTP example - Change working directory
- Java FTP create directory example
- Java FTP list files and directories example
- Java FTP file download tutorial and example
- Java FTP file upload tutorial and example
- Java FTP example - Search for files and directories
- How to upload a directory to a FTP server
- How to download a complete folder from a FTP server
Comments
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!
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.
It means server doesn't support MLST, right?
But the error message included "size" so How to filter only size on that ?
Thanks.