带有jsch的scp文件给出了'意外的文件名' [英] Scp file with jsch gives 'unexpected filename'
问题描述
我正在使用Jsch 0.1.44
将文件从一台主机发送到另一台主机.相关代码如下:
I'm using Jsch 0.1.44
to scp a file from one host to another. The relevant code is the following:
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);
看起来像这样:scp -t /tmp/config.xml
和该行中的命令
looks like this: scp -t /tmp/config.xml
and the command in this line
out.write(command.getBytes());
看起来像这样:C0644 5878 /home/myuser/config.xml
问题是,我从scp收到以下错误:scp: error: unexpected filename: /path/to/config.xml
The problem is, that I get the following error from scp: scp: error: unexpected filename: /path/to/config.xml
此错误的原因是什么?我该如何避免呢?
What is the reason for this error? How can I avoid it?
我们非常感谢您的帮助.
Any help is highly appreciated.
推荐答案
我找到了解决方案.似乎命令中的源文件名不能包含任何斜杠.要解决此问题,您只需更改以下行即可:
I've found a solution. It seems that the source filename in the command must not contain any slashes. To solve this problem you simple have to change this line:
command+=file;
对此:
command+=file.getName();
就这样.
这篇关于带有jsch的scp文件给出了'意外的文件名'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!