我使用Jsch 0.1.44
scp從一個主機到另一個文件的文件。相關的代碼如下:在此行帶jsch的scp文件給出'意想不到的文件名'
out.write(command.getBytes());
看起來像這樣scp -t /tmp/config.xml
和命令:
public boolean transferFileToHost(File fileToTransfer, String destDirectory, String destFilename) {
Channel channel = null;
try {
String command = "scp -t "+ destDirectory + destFilename;
channel = session.openChannel("exec");
((ChannelExec)channel).setCommand(command);
OutputStream out = channel.getOutputStream();
InputStream in = channel.getInputStream();
if(!connectToChannel(channel, in)) {
return false;
}
if(!sendScpCommand(fileToTransfer, command, out, in)) {
return false;
}
if(!sendFileContent(out, fileToTransfer, in)) {
return false;
}
return true;
} catch (IOException e) {
logger.error("Error while reading file. Error was: ",e);
} catch (JSchException e) {
logger.error("Error while sending ssh commands. Error was: ",e);
}
finally {
if(channel != null) {
channel.disconnect();
}
}
private boolean sendScpCommand(File file, String command, OutputStream out, InputStream in) throws IOException {
long filesize=file.length();
command="C0644 "+filesize+" ";
command+=file;
command+="\n";
out.write(command.getBytes());
out.flush();
if (checkAck(in) != 0) {
return false;
}
return true;
}
在這一行
((ChannelExec)channel).setCommand(command);
看起來像這樣的命令C0644 5878 /home/myuser/config.xml
問題是,我得到以下錯誤fr om scp:scp: error: unexpected filename: /path/to/config.xml
這個錯誤的原因是什麼?我怎樣才能避免它?
任何幫助,高度讚賞。
該命令是否需要'scp/path/to/local/file server:path/to/remote/file'?我不知道'-t'選項的作用。 – trojanfoe
scp -t執行遠程複製命令。我從jsch的例子中拿走了它。 – flash