How to save the date changed when retrieving a file using Apache FTPClient?

I use org.apache.commons.net.ftp.FTPClientto extract files from ftp server. It is imperative to save the last modified timestamp of a file when it is saved on my machine. Anyone have a suggestion how to solve this?

+4
source share
3 answers

Here's 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 to fulfill this function.

Here is how you can use it:

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

You can change the timestamp after downloading the file.

The timestamp can be obtained using the LIST command or the (non-standard) MDTM command.

, , : http://www.mkyong.com/java/how-to-change-the-file-last-modified-date-in-java/

+1

, , FTPClient.mlistDir FTPClient.listFiles, , , timestemp :

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

FTPClient.mdtmFile

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());
}
+1

All Articles