java 如何在远程服务器中将文件从目录 A 移动到目录 B?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30050045/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to move file from directory A to directory B in remote server?
提问by Jbisgood9999999
I am using JSch to connect to SFTP in a website which is made from GWT.
I had read a little example of sftpChannel.get()
, sftpChannel.rename()
, sftpChannel.rm()
我正在使用 JSch 连接到由 GWT 制作的网站中的 SFTP。我读过一个小例子sftpChannel.get()
, sftpChannel.rename()
,sftpChannel.rm()
But I didn't find a solution that copy a file from remote server a
directory to remote server b
directory.
但是我没有找到将文件从远程服务器a
目录复制到远程服务器b
目录的解决方案。
For example, I want to copy file from /appl/user/home/test/temp
to /appl/user/home/test/
. Filename = abc.jpg
.
例如,我要复制文件/appl/user/home/test/temp
到/appl/user/home/test/
。文件名 = abc.jpg
.
I stunned here for few hours since most of the solution from network is getting file from remote server to local, or uploading file from local to remote server.
我在这里愣了几个小时,因为大多数来自网络的解决方案是从远程服务器获取文件到本地,或者从本地上传文件到远程服务器。
String existingfile = "abc.jpg";
String newfile = "123.jpg";
FileDirectory = "/appl/user/home/test/";
sftp.cd(FileDirectory+"temp/");
sftp.rename(newfile, FileDirectory+newfile);
Let's say, abc.jpg
is existing in /appl/user/home/test/
假设abc.jpg
存在于/appl/user/home/test/
And I upload a 123.jpg
in /appl/user/home/test/temp/
.
我上传123.jpg
在/appl/user/home/test/temp/
。
Now, I want to move 123.jpg
to /appl/user/home/test/
and remove abc.jpg
in /appl/user/home/test/
.
现在,我想移动123.jpg
到/appl/user/home/test/
并删除abc.jpg
在/appl/user/home/test/
。
What should I do?
我该怎么办?
回答by Jbisgood9999999
It seems like SftpChannel.rename();
need to use full path of file instead of cd to the directory that the file I am going to move.
似乎SftpChannel.rename();
需要使用文件的完整路径而不是 cd 到我要移动的文件的目录。
String existingfile = "abc.jpg";
String newfile = "123.jpg";
FileDirectory = "/appl/user/home/test/";
sftp.cd(FileDirectory+"temp/");
if (sftp.get( newfile ) != null){
sftp.rename(FileDirectory + "temp/" + newfile ,
FileDirectory + newfile );
sftp.cd(FileDirectory);
sftp.rm(existingfile );
}
回答by Martin Prikryl
A core SFTP protocol does not support duplicating a remote file.
核心 SFTP 协议不支持复制远程文件。
There's draft of copy-file
extension to the protocol, but that's supported by only few SFTP servers (ProFTPD/mod_sftpand Bitvise SFTP server for example).
copy-file
协议有扩展草案,但只有少数 SFTP 服务器支持(例如ProFTPD/mod_sftp和 Bitvise SFTP 服务器)。
The JSch library does not support the copy-file
extension either.
JSch 库也不支持该copy-file
扩展。
Alternatives:
备择方案:
- If you have a shell access, open an "exec" channel, and use shell
cp
command (or equivalent command for your server's OS).
SeeExec.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 the streams, to avoid a temporary file.
- 如果您有 shell 访问权限,请打开“exec”通道,并使用 shell
cp
命令(或服务器操作系统的等效命令)。
参见Exec.java
示例。 - 否则,您唯一的选择是将文件下载到本地临时位置并将其副本上传回不同的/目标远程目录。或者使用流,以避免临时文件。
See also How can I copy/duplicate a file to another directory using SFTP?
回答by Kushal Shukla
You can write a normal Java FileInputStream
and FileOutputStream
code and instead of using paths like these /appl/user/home/test/temp
use full path with its IpAddress or remote server name + your path eg myremoteserver/appl/user/home/test/temp
您可以编写普通的 JavaFileInputStream
和FileOutputStream
代码,而不是使用这些路径,而是/appl/user/home/test/temp
使用完整路径及其 IpAddress 或远程服务器名称 + 您的路径,例如myremoteserver/appl/user/home/test/temp