Java的。获取上次文件修改的日期

时间:2014-07-07 11:00:21

标签: java ftp timestamp

我尝试使用FTPClient类获取上次文件修改的日期:

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

在某些文件中,打印时间不正确:它是00:00而不是真值。我的错误在哪里,我该如何解决?可能还有其他方法可以获取此信息吗?

我尝试使用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();
}

时间为00:00FTPClient课程的文件现在根本没有。我的错误在哪里?可能是它取决于文件类型?

非常感谢!

2 个答案:

答案 0 :(得分:2)

答案 1 :(得分:0)

某些FTP服务器支持public String getModificationTime(String pathname)FTPClient。也许你可以尝试这种方法。