Using SFTP in Java, How do I transfer a file from one folder to another? [closed]

蓝咒 提交于 2019-11-28 13:14:08

There's no native SFTP support in Java.

The JSch library, you have found, is probably the most widely used SFTP implementation for Java.


If you want to move the file from the SFTP_1 to the SFTP_2 using JSch, use the ChannelSftp.rename method:

channelSftp.rename("/SFTP_1/file.txt", "/SFTP_2/file.txt");

If you want to copy the file, it's more complicated. While there's the copy-file extension to the SFTP protocol, it's supported by only a few SFTP servers. And it's not supported by the JSch library either.

So in the end, your only option is probably 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). Or use shell session to invoke a command like cp. See also

SFTP supports RenameFile and CopyRemoteFile operations but only starting from SFTP protocol version 5 or 6 if memory serves. Our product, SecureBlackbox (Java edition), supports these operations, however it is necessary to ensure that your server supports the required SFTP version and thus the file operations you need.

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