在Java中使用SFTP,如何将文件从一个文件夹传输到另一个文件夹? [英] Using SFTP in Java, How do I transfer a file from one folder to another?

查看:560
本文介绍了在Java中使用SFTP,如何将文件从一个文件夹传输到另一个文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含两个文件夹的简单目录 -

I have a simple directory with two folders -

SFTP_1 文件夹中,我有一个位图图像。而 SFTP_2 文件夹只是一个空文件夹。

In the SFTP_1 folder, I have a bitmap image. And the SFTP_2 folder is just an empty folder.

Java是否有要使用的本机SFTP库?当我搜索时,我只在网上找到一个名为 JSch 的图书馆。

Does Java have a native SFTP library to use? When I searched I only found a library online called JSch.

我将如何开始运行此示例?
任何提示赞赏,
谢谢!

How would I get started to run this example? Any tips appreciated, thanks!

推荐答案

Java中没有本机SFTP支持。

There's no native SFTP support in Java.

您发现,JSch库可能是Java中使用最广泛的SFTP实现。

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

如果要使用JSch将文件从 SFTP_1 移动到 SFTP_2 ,请使用 ChannelSftp.rename 方法

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");






如果你想复制文件,它会更多复杂。虽然有 副本-file SFTP协议的扩展,仅少数SFTP服务器支持。并且JSch库也不支持它。


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.

所以最后,你唯一的选择可能是将文件下载到本地临时文件夹并将其上传回新位置(或使用流,以避免临时文件)。或者使用shell会话来调用 cp 之类的命令。另请参阅

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

  • How do I transfer a file from one directory to another using Java SFTP Library JSch?
  • How do I copy files stored in a remote SFTP server to another folder in the same remote server using Java?

这篇关于在Java中使用SFTP,如何将文件从一个文件夹传输到另一个文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆