我使用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