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:
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:
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:
Nam Ha Minh 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.
Comments