使用 Apache FTPClient 检索文件时如何保留修改日期? [英] How to preserve date modified when retrieving file using Apache FTPClient?

查看:67
本文介绍了使用 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());
}

这篇关于使用 Apache FTPClient 检索文件时如何保留修改日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆