In Java FTP programming with Apache Commons Net API, to know details of a file or directory such as size and last modified date (timestamp) from a FTP server, we can use the mlistFile() method of the FTPClient class as follows:

FTPFile ftpFile = ftpClient.mlistFile(remoteFilePath);

Where remoteFilePath is a String represents path of the file or directory (can be relative or absolute to the user’s home directory on the server). If the specified file/directory exists, the method returns a FTPFile object, otherwise it returns null. Then we can use some getter methods of the FTPFile class to get desired information as follows:

    • String getGroup(): gets name of the group owning the file.
    • String getName(): gets name of the file.
    • String getRawListing(): gets server raw listing of the file.
    • long getSize(): gets file size in bytes.
    • Calendar getTimestamp(): gets timestamp.
    • int getType(): gets type of the file.
    • String getUser(): gets name of the user owning the file.

The following is code of an example program that logins to a FTP server then gets details of a file using the mlistFile() method:

package net.codejava.ftp;

import java.io.IOException;

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

/**
 * An example program that demonstrates how to get details of a file and
 * directory from a FTP server using Apache Commnons Net API.
 * @author www.codejava.net
 *
 */
public class FTPGetFileDetails {

	public static void main(String[] args) {
		String server = "www.myserver.com";
		int port = 21;
		String user = "username";
		String pass = "password";

		FTPClient ftpClient = new FTPClient();

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

			// use local passive mode to pass firewall
			ftpClient.enterLocalPassiveMode();

			// get details of a file or directory
			String remoteFilePath = "Java/CodeLib/FTP.rar";

			FTPFile ftpFile = ftpClient.mlistFile(remoteFilePath);
			if (ftpFile != null) {
				String name = ftpFile.getName();
				long size = ftpFile.getSize();
				String timestamp = ftpFile.getTimestamp().getTime().toString();
				String type = ftpFile.isDirectory() ? "Directory" : "File";

				System.out.println("Name: " + name);
				System.out.println("Size: " + size);
				System.out.println("Type: " + type);
				System.out.println("Timestamp: " + timestamp);
			} else {
				System.out.println("The specified file/directory may not exist!");
			}

			ftpClient.logout();
			ftpClient.disconnect();

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

Here’s the output when running the above program:

Name: Java/CodeLib/FTP.rar
Size: 13021440
Type: File
Timestamp: Sat Jul 13 13:11:04 ICT 2013 

 

API References:

 

Related Java FTP Tutorials:

 

Other Java FTP Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on Facebook and watch his Java videos you YouTube.



Attachments:
Download this file (FTPGetFileDetails.java)FTPGetFileDetails.java[Java FTP get file details example program]1 kB

Add comment

   


Comments 

#9Nam2020-08-09 20:36
Hi Boris Maidana,
You need to use Java File IO to read the text file. Refer to this guide: codejava.net/.../...
Quote
#8Boris Maidana2020-08-09 11:23
How can I read the text inside of the txt? Greetings
Quote
#7SVK PETO2020-02-25 09:21
I got null
Server Reply is:
>500 Command not understood.
is it only wrong path?
Quote
#6Nam2013-10-03 00:01
Null means that the file does not exist or the user doesn't have permission. Probably you stil have wrong path. Try to test a hard coded path.
Quote
#5xugang2013-10-02 20:22
Thank you

But Still return null :

FTPClient ftp = new FTPClient ();
.......
String[] names=ftp.listNames();//normal,return ten names
for(String name : names){
String pn=ftp.printWorkingDirectory();
pn+="/";
pn+=name;
FTPFile ftpf=ftp.mlistFile(pn);//Anyway,return null still
}
why?
Quote