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!