使用 Apache FTPClient 检索文件时如何保留修改日期?

我正在使用 org.apache.commons.net.ftp.FTPClient 从 ftp 服务器检索文件.当文件保存在我的机器上时,我保留文件上最后修改的时间戳是至关重要的.有人对如何解决这个问题有建议吗?

I am using org.apache.commons.net.ftp.FTPClient for retrieving files from a ftp server. It is crucial that I preserve the last modified timestamp on the file when its saved on my machine. Do anyone have a suggestion for how to solve this?

推荐答案

我是这样解决的:

public boolean retrieveFile(String path, String filename, long lastModified) throws IOException {
    File localFile = new File(path + "/" + filename);
    OutputStream outputStream = new FileOutputStream(localFile);
    boolean success = client.retrieveFile(filename, outputStream);
    outputStream.close();
    localFile.setLastModified(lastModified);
    return success;
}

我希望 Apache 团队能够实现此功能.

I wish the Apache-team would implement this feature.

你可以这样使用它:

List<FTPFile> ftpFiles = Arrays.asList(client.listFiles());
for(FTPFile file : ftpFiles) {
    retrieveFile("/tmp", file.getName(), file.getTimestamp().getTime());
}

相关文章