Copying files internally on an SFTP server using Spring integration/JCraft JSch


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!


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:

Answered By – Martin Prikryl

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published