How do I copy files stored in a remote SFTP server to another folder in the same remote server using Java?

不羁的心 提交于 2019-12-30 11:42:19

问题


I have been trying to copy few selected files (after performing few checks), in a remote server to the same remote server using:

File localFile = new File(srcPath);
sftpChannel.put(localFile.getAbsolutePath(),localFile.getName());

I have even tried to copy those selected files to my local machine using the get() method.

Can somebody help?


回答1:


A core SFTP protocol does not support duplicating a remote file.

There's 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).

The JSch library does not support the copy-file extension either.

Alternatives:

  • If you have a shell access, open an "exec" channel, and use shell cp command (or equivalent command for your server's OS).
    See Exec.java example.
  • Otherwise, your only option is to download the file to a local temporary location and upload its copy back to a different/target remote directory. Or use streams, to avoid a temporary file.

See also How can I copy/duplicate a file to another directory using SFTP?



来源:https://stackoverflow.com/questions/45004008/how-do-i-copy-files-stored-in-a-remote-sftp-server-to-another-folder-in-the-same

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!