如何将文件从远程服务器的目录A移动到目录B?

How to move file from directory A to directory B in remote server?

我正在使用 JSch 连接到一个由 GWT 制作的网站中的 SFTP。 我读过 sftpChannel.get()sftpChannel.rename()sftpChannel.rm()

的一个小例子

但我没有找到将文件从远程服务器 a 目录复制到远程服务器 b 目录的解决方案。

例如,我想将文件从 /appl/user/home/test/temp 复制到 /appl/user/home/test/。文件名 = abc.jpg.

我在这里愣了几个小时,因为大多数网络解决方案都是从远程服务器获取文件到本地,或者从本地上传文件到远程服务器。

String existingfile = "abc.jpg";
String newfile = "123.jpg";
FileDirectory = "/appl/user/home/test/";
sftp.cd(FileDirectory+"temp/");
sftp.rename(newfile, FileDirectory+newfile);

假设 abc.jpg 存在于 /appl/user/home/test/

然后我在 /appl/user/home/test/temp/ 中上传了 123.jpg

现在,我想将 123.jpg 移动到 /appl/user/home/test/ 并删除 /appl/user/home/test/ 中的 abc.jpg

我该怎么办?

您可以编写正常的 Java FileInputStreamFileOutputStream 代码,而不是使用像这样的路径 /appl/user/home/test/temp 使用完整路径及其 IpAddress 或远程服务器名称 +你的路径例如 myremoteserver/appl/user/home/test/temp

似乎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 );
}