我正在尝试从 FTP 环境中获取文件的最后修改日期。结果与预期不同。
通过使用 ftpClient.getModificationTime("File path")
我得到了 null。
通过使用 FTPFile.getTimestamp().getTime()
我得到了错误的上次修改时间(即真正的上次修改时间是今天,我得到的是 Wed 2004 年 2 月 18 日 02:55:22 EST)。
如何获得正确的最后修改时间? File at FTP
提前致谢。
最佳答案
FTPClient.getModificationTime
服务器对MDTM
命令返回错误响应时返回null
。通常这意味着:
- “文件路径”不存在;或
- FTP 服务器不支持
MDTM
命令。
检查 FTPClient.getReplyString()
.
如果发现 FTP 服务器不支持 MDTM
命令,您将不得不使用其他方法来检索时间戳。如果不支持 MDTM
,则 MLSD
也不会。
在这种情况下,唯一的其他方法是使用 LIST
命令检索所有文件的列表并查找您需要的文件 - 使用 FTPClient.listFiles()
.
FTPFile[] remoteFiles = ftpClient.listFiles(remotePath);
Arrays.sort(remoteFiles,
Comparator.comparing((FTPFile remoteFile) -> remoteFile.getTimestamp()).reversed());
FTPFile latestFile = remoteFiles[0];
System.out.println(
"Latest file is " + latestFile.getName() +
" with timestamp " + latestFile.getTimestamp().getTime().toString());
另见 Make FTP server return files listed by timestamp with Apache FTPClient .
关于java - 使用 FTPClient.getModificationTime 获取 FTP 服务器中文件的最后修改日期会产生 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49623415/