[ACCEPTED]-Retaining the last modified date while using ChannelSftp for file transfer-jsch
Accepted answer
Using a SimpleDateFormat
is of less performance. Instead 6 one could use the methods getMTime()
and getATime()
directly. But 5 they deliver a value reduced by the milliseconds.
That's 4 why they return an int
and not a long
as expected 3 in conformance to date.getTime()
.
SftpATTRS attrs = lsEntry.getAttrs();
Date dateModify = new Date(attrs.getMTime() * 1000L);
Date dateAccess = new Date(attrs.getATime() * 1000L);
In jsch-0.1.50 be careful of using 2 getAtimeString()
there is the factor 1000L
missing.
In jsch-0.1.51 the getAtimeString()
bug 1 with the missing factor is fixed.
This changes the last modified time of the 1 file downloaded from the remote server,
String remoteFilePath = "testDir/testFile.txt";
SftpATTRS attrs = sftpChannel.lstat(remoteFilePath);
SimpleDateFormat format = new SimpleDateFormat(
"EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
Date modDate = (Date) format.parse(attrs.getMtimeString());
String localFilePath = "C:/temp/downloadedFile.txt";
sftpChannel.get(remoteFilePath, localFilePath);
File downloadedFile = new File(localFilePath);
downloadedFile.setLastModified(modDate.getTime());
Merging the answers above to a working solution:
sftpChannel.get(REMOTE_FILE, LOCAL_FILE);
SftpATTRS attrs = sftpChannel.lstat(REMOTE_FILE);
Date dateModify = new Date(attrs.getMTime() * 1000L);
File downloadedFile = new File(LOCAL_FILE);
downloadedFile.setLastModified(dateModify.getTime())
0
Source:
stackoverflow.com
More Related questions
Cookie Warning
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.