Java FTP example - Get and set file modification time
- Details
- Written by Nam Ha Minh
- Last Updated on 19 July 2019   |   Print Email
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:
- Determine if a directory or file exists on FTP server
- Get size of a file on FTP server
- 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