1

I need to preserve modified timestamp of my file while uploading it to SFTP site.

I am using com.enterprisedt.net.ftp.ssh.SSHFTPclient for the transfer.

Please let me know if there is any way.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992

2 Answers2

2

Let me know if there is any way.

There is no way to preserve the timestamps while transferring the file. The SFTP protocol does not allow it.

The SFTP wire protocol specification documents were never completed and ratified, but the most recent draft (for version 6 of the protocol) is draft-ietf-secsh-filexfer-13.

If you look at Section 8.1 and 8.2, you will see that the client requests for opening a file (SSH_FXP_OPEN) and writing a file (SSH_FXP_WRITE) do not pass any time-stamp information from the client to the server. Since the request doesn't pass timestamps, they cannot be preserved in the transfer.

If you want to "preserve" the timestamp, the protocol document says that the client needs to send an SSH_FXP_SETSTAT or SSH_FXP_FSETSTAT request to update the relevant file attributes.

Martin's Answer has code for doing this using the SSHFTPClient library.

I have not checked, but I am confident that this applies to the earlier versions of the SFTP protocol as well.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
1

Call SSHFTPClient.setModTime after the upload.

sshFtpClient.setModTime(remotePath, new Date(new File(localPath).lastModified());
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
  • Your are explicitly setting the original timestamp after the upload.that is fine . But I am asking with the explicit setting is there any method or parameter that will setup the client to stick with original timestamp for all the upload files for current connection – Ramya Murugesan Feb 28 '20 at 14:06
  • I do not think so. Even if it where, it would internally do the same. – Martin Prikryl Feb 28 '20 at 14:45