In Java FTP programming with Apache Commons Net API, it’s possible to get and set modification time of a specific file on FTP server, using the following methods of the FTPClient class:

    • String getModificationTime(String filePath)
    • boolean setModificationTime(String filePath, String time)
The FTP server returns time in the format of YYYYMMDDhhmmss(ISO 3077) which is equivalent to yyyyMMddHHmmss as in Java date time pattern.

 

NOTES: The FTP server must support the MDTM (get) and MFMT (set) commands in order to get these methods take effect. Not every FTP server supports these commands, so you may need to query the server’s features before using this methods.

 

Get file modification time example:

Here’s an example that gets modification time of a given file:

String filePath = "Upload/Picture.png";
String time = ftpClient.getModificationTime(filePath);
System.out.println("Server Reply: " + time);
The output would be:

Server Reply: 213 20130417033333
Note that the getModificationTime() method gives back a String containing server reply code (213) and the actual time (20130417033333), so we need to do a little more work to get the actual time value. Here’s a small method that takes the time string and converts it to a regular Date object in Java:

void printTime(String time) {
	SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
	try {
		String timePart = time.split(" ")[1];
		Date modificationTime = dateFormat.parse(timePart);
		System.out.println("File modification time: " + modificationTime);
	} catch (ParseException ex) {
		ex.printStackTrace();
	}
}
 

This method would give the following output:

File modification time: Wed Apr 17 03:33:33 ICT 2013 


 

Set file modification time example:

Here’s an example that sets modification time of a given file on the server:

String filePath = "Upload/Picture.png";
String time = "20130816162432";
ftpClient.setModificationTime(filePath, time);
System.out.println(ftpClient.getReplyString());
Output (server’s reply):

213 UTIME OK 
 

Java FTP Set File Time Example program:

And the following is a complete example program that connects to a FTP server, then gets and sets modification time of a file:

package net.codejava.ftp;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

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

/**
 * This program demonstrates how to get and set modification time
 * of a specific file on a FTP server using Apache Commons Net API.
 * @author www.codejava.net
 *
 */
public class FTPModificationTimeDemo {

	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);

			ftpClient.enterLocalPassiveMode();

			// get modification time
			String filePath = "Upload/Picture.png";

			String time = ftpClient.getModificationTime(filePath);

			System.out.println("Server Reply: " + time);

			printTime(time);

			// set modification time
			time = "20130816162432";
			ftpClient.setModificationTime(filePath, time);
			System.out.println(ftpClient.getReplyString());

			ftpClient.logout();

			ftpClient.disconnect();

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

	static void printTime(String time) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
		try {
			String timePart = time.split(" ")[1];
			Date modificationTime = dateFormat.parse(timePart);
			System.out.println("File modification time: " + modificationTime);
		} catch (ParseException ex) {
			ex.printStackTrace();
		}
	}
}
Output:

Server Reply: 213 20130417033333

File modification time: Wed Apr 17 03:33:33 ICT 2013
213 UTIME OK
 

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 (FTPModificationTimeDemo.java)FTPModificationTimeDemo.java[Demo program]1 kB

Add comment

   


Comments 

#1Prem Nath2018-04-21 04:35
How to get base64 of FTP file in java?
Quote