使用 JSCH 将文件从一台远程服务器发送到另一台使用 JSCH 的服务器 [英] Send files from one remote server using JSCH to another server using JSCH too
本文介绍了使用 JSCH 将文件从一台远程服务器发送到另一台使用 JSCH 的服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将文件从我的第一个远程服务器发送到另一个:
I would like to send files from my first remote server to another one:
public boolean uploadFile() throws JSchException, SftpException {
ChannelSftp channelSftpA = createChannelSftp();
ChannelSftp channelSftpB = createChannelSftp();
channelSftpA.connect();
channelSftpB.connect();
localFilePath = "/data/upload/readme.txt";
remoteFilePath = "/bingo/pdf/";
channelSftpA.cd(localFilePath);
channelSftpA.put(localFilePath + "readme.txt", remoteFilePath + "readme.txt");
但它不起作用.我应该将 channelB.put
放入我的第一个 channelA.put
中吗?
But it doesn't work. Should I put channelB.put
into my first channelA.put
?
推荐答案
如果我理解你的问题是正确的,你的代码将从第三台服务器运行,为了传输文件,你应该从 服务器 A
获取文件,然后放在 server B
上.顺便说一下,您要下载和上传文件的用户应该有权访问指定的文件夹!
If I understood your question correct, you code will be run from third server, for transferring file you should get file from server A
, and that put on server B
. By the way users under which you are going to download and upload files should have access to specified folders!
private boolean transferFile() throws JSchException, SftpException {
ChannelSftp channelSftpA = createChannelSftp();
ChannelSftp channelSftpB = createChannelSftp();
channelSftpA.connect();
channelSftpB.connect();
String fileName = "readme.txt";
String remoteFilePathFrom = "/folderFrom/";
String remoteFilePathTo = "/folderTo/";
InputStream srcInputStream = channelSftpA.get(remoteFilePathFrom + fileName);
channelSftpB.put(srcInputStream, remoteFilePathTo + fileName);
System.out.println("Transfer has been completed");
channelSftpA.exit();
channelSftpB.exit();
return true;
}
这篇关于使用 JSCH 将文件从一台远程服务器发送到另一台使用 JSCH 的服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文