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
我將它作爲遠程主機的標準輸出 –