问题
I was wondering if there is a way to copy files on an SFTP server to another directory on the same SFTP server. I want to do this without getting the file in a client and then setting it in the other folder. Of course this would work fine but I guess that this would produce more overhead, so I would like to avoid this if at all possible. I'm currently working with Spring integration which is based on JCraft JSch.
So far I haven't been able to find any way to do this without an intermediary.
Another approach would be to open an SSH channel and just use the cp
command but well that's not too pretty either in my opinion.
Thanks in advance!
回答1:
A core SFTP protocol does not support duplicating a remote file.
There's a draft of copy-file extension to the protocol, but that's supported by only few SFTP servers (ProFTPD/mod_sftp and Bitvise SFTP server for example).
It's definitely not supported by the most widespread OpenSSH SFTP server.
And it's also not supported by the JSch library.
See also my answer to How can I copy/duplicate a file to another directory using SFTP?
So actually using the cp
shell command over an "exec" channel (ChannelExec) is unfortunately the best available approach (assuming you connect to a *nix server and you have a shell access).
If you do not have a shell access, then your only option is indeed to download the file to a local temporary folder and upload it back to the new location (or use streams, to avoid a temporary file). See also:
- How do I transfer a file from one directory to another using Java SFTP Library JSch?
- How do I copy files stored in a remote SFTP server to another folder in the same remote server using Java?
来源:https://stackoverflow.com/questions/37664612/copying-files-internally-on-an-sftp-server-using-spring-integration-jcraft-jsch