How to preserve date modified when retrieving file using Apache FTPClient?

蓝咒 提交于 2020-01-11 05:59:28

问题


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?


回答1:


This is how I solved it:

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

I wish the Apache-team would implement this feature.

This is how you can use it:

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



回答2:


You can modify the timestamp after downloading the file.

The timestamp can be retrieved through the LIST command, or the (non standard) MDTM command.

You can see here how to do modify the time stamp: that: http://www.mkyong.com/java/how-to-change-the-file-last-modified-date-in-java/




回答3:


When download list of files, like all files returned by by FTPClient.mlistDir or FTPClient.listFiles, use the timestamp returned with the listing to update timestemp of local downloaded files:

String remotePath = "/remote/path";
String localPath = "C:\\local\\path";

FTPFile[] remoteFiles = ftpClient.mlistDir(remotePath);
for (FTPFile remoteFile : remoteFiles) {
    File localFile = new File(localPath + "\\" + remoteFile.getName());

    OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(localFile));
    if (ftpClient.retrieveFile(remotePath + "/" + remoteFile.getName(), outputStream))
    {
        System.out.println("File " + remoteFile.getName() + " downloaded successfully.");
    }
    outputStream.close();

    localFile.setLastModified(remoteFile.getTimestamp().getTimeInMillis());
}

When downloading a single specific file only, use FTPClient.mdtmFile to retrieve the remote file timestamp and update timestamp of the downloaded local file accordingly:

File localFile = new File("C:\\local\\path\\file.zip");
FTPFile remoteFile = ftpClient.mdtmFile("/remote/path/file.zip");
if (remoteFile != null)
{
    OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(localFile));
    if (ftpClient.retrieveFile(remoteFile.getName(), outputStream))
    {
        System.out.println("File downloaded successfully.");
    }
    outputStream.close();

    localFile.setLastModified(remoteFile.getTimestamp().getTimeInMillis());
}


来源:https://stackoverflow.com/questions/27013672/how-to-preserve-date-modified-when-retrieving-file-using-apache-ftpclient

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!