3
我见过很多JSch的例子,可以从本地机器复制到远程机器,反之亦然。如果我能够为两台远程机器提供登录凭证,那么我可以在JSch中从一台远程机器复制到另一台远程机器。使用JSch从远程机器复制一个文件到另一个远程机器
我曾尝试下面的代码
JSch jsch=new JSch();
Session session=jsch.getSession(user1, host1, 22);
// username and password will be given via UserInfo interface.
UserInfo ui=new MyUserInfo(password);
session.setUserInfo(ui);
session.connect();
Session remoteSession = jsch.getSession(user2, host2, 22);
UserInfo ui2 = new MyUserInfo(password2);
remoteSession.setUserInfo(ui2);
remoteSession.connect();
// exec 'scp -f rfile' remotely
String command="scp -f "+lfile;
Channel channel=session.openChannel("exec");
((ChannelExec)channel).setCommand(command);
Channel channel2=session.openChannel("exec");
((ChannelExec)channel2).setCommand(command);
// get I/O streams for remote scp
OutputStream out=channel.getOutputStream();
InputStream in=channel2.getInputStream();
channel.connect();
channel2.connect();
从输入流中读取后挂起。请让我知道如何实现远程复制。
尝试过,但它不工作:(任何其他想法?给出错误说该文件不存在 –
当您调用SCP时,您是否将Java异常或远程主机A的标准输出中的输出看到?如果是前者,我认为你的解决方案不符合我的建议。 – Kabron
我将它作为远程主机的标准输出 –