简体   繁体   English

使用 Spring 集成/JCraft JSch 在 SFTP 服务器上内部复制文件

[英]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.我想知道是否有办法将 SFTP 服务器上的文件复制到同一 SFTP 服务器上的另一个目录。 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.我目前正在使用基于 JCraft JSch 的 Spring 集成。

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.另一种方法是打开一个 SSH 通道并只使用cp命令,但在我看来这也不太漂亮。

Thanks in advance!提前致谢!

A core SFTP protocol does not support duplicating a remote file.核心 SFTP 协议不支持复制远程文件。

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). 该协议有一个copy-file扩展草案,但只有少数 SFTP 服务器(例如ProFTPD mod_sftp和 Bitvise SFTP 服务器)支持。

In the most widespread OpenSSH SFTP server it is supported only by very recent version 9.0 .在最广泛使用的 OpenSSH SFTP 服务器中,只有最近的9.0 版本才支持它。

And it's also not supported by the JSch library. JSch 库也不支持它。


See also my answer to How can I copy/duplicate a file to another directory using SFTP?另请参阅我对如何使用 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).因此,不幸的是,实际上在“exec”通道( ChannelExec )上使用cp shell 命令是最好的可用方法(假设您连接到 *nix 服务器并且您具有 shell 访问权限)。


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).如果您没有 shell 访问权限,那么您唯一的选择确实是将文件下载到本地临时文件夹并将其上传回新位置(或使用流,以避免临时文件)。 See also:也可以看看:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM