1

I try to get date of last file modification using FTPClient class:

FTPClient client = new FTPClient();
client.connect(hostname);
client.login(user, passwd);
client.changeWorkingDirectory(dir);
FTPFile[] files = client.listFiles();

for (FTPFile file : files) {
    SimpleDateFormat formatDate = new SimpleDateFormat("dd.MM.yyyy HH:mm");
    String formattedDate = formatDate.format(file.getTimestamp().getTime());
    System.out.println(file.getName() + "     " + file.getSize() + "     " + formattedDate);
}

At some files time is not printing correct: it is 00:00 instead of true value. Where is my mistake and how can I solve it? May be there are other methods to get this information?

I tried to implement same procedure using URLConnection:

String ftpUrl = "ftp://%s:%s@%s/%s";
String host = "ip address";
String user = "user name";
String pass = "password";
String filePath = "/folder name/";

ftpUrl = String.format(ftpUrl, user, pass, host, filePath);
System.out.println("URL: " + ftpUrl);
try {
    URL url = new URL(ftpUrl);
    URLConnection conn = url.openConnection();

    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    String line;         // read from the urlconnection via the bufferedreader
    while ((line = bufferedReader.readLine()) != null) {
         System.out.println(line);
    }
} catch (IOException ex) {
    ex.printStackTrace();
}

Files which had time 00:00 with FTPClient class now don't have it at all. Where is my mistake? May be it depends on file type?

Thank you very much!

user3649515
  • 199
  • 7
  • 17
  • Propably should tell us which FTPClient you're using? Guessing it's Apache commons-net? Have you checked the return value from `file.getTimeStamp()` and `file.getTimeStamp().getTime()` to verify what time they think it is? – Peter Svensson Jul 07 '14 at 11:08
  • Peter Liljenberg, yes. I used commons-net-3.3. `file.getTimeStamp().getTime()` returns something like `Sun Jun 01 00:00:00 EEST 2014`. – user3649515 Jul 07 '14 at 11:09
  • `file.getTimestamp()` returns `java.util.GregorianCalendar[time=1401570000000,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo` – user3649515 Jul 07 '14 at 11:17
  • `[id="Europe/Athens",offset=7200000,dstSavings=3600000,useDaylight=true,transitions=138,lastRule=java.util.SimpleTimeZone[id=Europe/Athens,offset=7200000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=2,startMonth=2,startDay=-` – user3649515 Jul 07 '14 at 11:17
  • `1,startDayOfWeek=1,startTime=3600000,startTimeMode=2,endMode=2,endMonth=9,endDay=-1,endDayOfWeek=1,endTime=3600000,endTimeMode=2]],firstDayOfWeek=2,minimalDaysInFirstWeek=1,ERA=1,YEAR=2014,MONTH=5,WEEK_OF_YEAR=22,WEEK_OF_MONTH=1,DAY_OF_MONTH=1,DAY_OF_YEAR=152,DAY_OF_WEEK=1,DAY_OF_WEEK_IN_MONTH=1,AM_PM=0,HOUR=0,HOUR_OF_DAY=0,MINUTE=0,SECOND=0,MILLISECOND=0,ZONE_OFFSET=7200000,DST_OFFSET=3600000]` – user3649515 Jul 07 '14 at 11:18
  • `file.getTimestamp().getTimeInMillis()` returns `1401570000000` – user3649515 Jul 07 '14 at 11:23

2 Answers2

2

Have a look at this Java FTP example - Get and set file modification time

OO7
  • 2,785
  • 1
  • 21
  • 33
  • user000324, how can access file using ip address and directory? – user3649515 Jul 07 '14 at 11:44
  • Try this `File inputFile = new File("\\your IP\path to file");` – OO7 Jul 07 '14 at 12:07
  • I used this string instead of `filepath` in arguments of method `getModificationTime`: `ftp://user_name:password@ip_address//directory//` but the return result is `NULL` – user3649515 Jul 07 '14 at 12:08
  • From where u r accessing file, locally or remotely ? – OO7 Jul 07 '14 at 12:12
  • Read this post http://stackoverflow.com/questions/12615475/basics-reading-writing-remote-files-using-java – OO7 Jul 07 '14 at 12:30
  • I found how looks connection string but it doesn't help. I edited my post (question) – user3649515 Jul 07 '14 at 12:37
  • Have you tried this method of FTPClient `getLastModificationTimeDate(java.lang.String filePath)` ? It returns last modification or update time of a file or a folder as a java.util.Date object. – OO7 Jul 07 '14 at 13:13
  • I need to use zehon transfer pack? – user3649515 Jul 07 '14 at 13:33
  • No, it doesn't help. Result is the same. – user3649515 Jul 07 '14 at 14:02
  • See [Read file attributes using FTP protocol](http://stackoverflow.com/questions/24569861/java-read-file-attributes-using-ftp-protocol) post where @Goibniu told, **this code is susceptible to SQL injection attack**. Okey, look at this article [Java FTP example - Get details of a file or directory on server](http://www.codejava.net/java-se/networking/ftp/java-ftp-example-get-details-of-a-file-or-directory-on-server). Now I hope you will get the solution. – OO7 Jul 08 '14 at 05:14
0

Some FTP servers support public String getModificationTime(String pathname) on the FTPClient class. Maybe you could try this approach.

Wojciech Owczarczyk
  • 5,595
  • 2
  • 33
  • 55