如何将文件从目录A移动到远程服务器中的目录B? [英] How to move file from directory A to directory B in remote server?

查看:76
本文介绍了如何将文件从目录A移动到远程服务器中的目录B?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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()

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

But I didn't find a solution that copy a file from remote server a directory to remote server b directory.

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

For example, I want to copy file from /appl/user/home/test/temp to /appl/user/home/test/. Filename = 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);

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

我上传 123。 jpg / appl / user / home / test / temp /

现在,我想将 123.jpg 移至 / appl / user / home / test / 并删除 abc.jpg in / appl / user / home / test /

Now, I want to move 123.jpg to /appl/user/home/test/ and remove abc.jpg in /appl/user/home/test/.

我该怎么办?

推荐答案

好像是 SftpChannel.rename(); 需要使用文件的完整路径而不是cd到我要移动的文件的目录。

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.

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

这篇关于如何将文件从目录A移动到远程服务器中的目录B?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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