How do I transfer a file from one directory to another using Java SFTP Library JSch?

前端 未结 4 603
夕颜
夕颜 2020-11-30 11:36

I need to program a file transfer using JSch library. I have a simple directory with two folders -

In the SFTP_1 folder, I have a bitmap image

4条回答
  •  广开言路
    2020-11-30 11:57

    A core SFTP protocol does not support duplicating remote files.

    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). This is what the accepted answer by @Edwin Buck shows.

提交回复
热议问题