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

前端 未结 3 1135
情歌与酒
情歌与酒 2021-01-14 02:36

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 wh

3条回答
  •  难免孤独
    2021-01-14 03:16

    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 ftpFiles = Arrays.asList(client.listFiles());
    for(FTPFile file : ftpFiles) {
        retrieveFile("/tmp", file.getName(), file.getTimestamp().getTime());
    }
    

提交回复
热议问题