gpt4 book ai didi

java - 带有 jsch 的 scp 文件给出 'unexpected filename'

转载 作者:行者123 更新时间:2023-11-30 04:57:45 25 4
gpt4 key购买 nike

我正在使用 Jsch 0.1.44 将文件从一台主机 scp 到另一台主机。相关代码如下:

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 以及这一行中的命令

out.write(command.getBytes());

看起来像这样:C0644 5878/home/myuser/config.xml

问题是,我从 scp 收到以下错误:scp: error: Unexpected filename:/path/to/config.xml

这个错误的原因是什么?我怎样才能避免它?

非常感谢任何帮助。

最佳答案

我已经找到解决办法了。看来命令中的源文件名不能包含任何斜杠。要解决这个问题,您只需更改这一行:

command+=file;

进入此:

command+=file.getName();

就是这样。

关于java - 带有 jsch 的 scp 文件给出 'unexpected filename',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7991794/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com