JSch没有将完整的文件上传到远程SFTP服务器,只有部分

问题描述 投票:0回答:1

我正在尝试使用Jsch库将本地创建的XML文件(使用JAXB从Java对象编组)传输到远程服务器。但是,该文件仅部分上传。它缺少结尾标记,结尾缺少任意数量的字符。

我的代码看起来像这样(TradeLimits是一个带有JAXB注释的Java类)

TradeLimits limits = getTradeLimits(); //complex object with many fields
JSch jsch = new JSch();
jschSession = jsch.getSession(username, remoteHost);

//to avoid unknown host issues
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
jschSession.setConfig(config);

jschSession.setPassword(password);
jschSession.setPort(22);
jschSession.connect();

ChannelSftp channelSftp = (ChannelSftp) jschSession.openChannel("sftp");
channelSftp.connect();

jaxbContext = JAXBContext.newInstance(TradeLimits.class);           
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); //for pretty print
marshaller.marshal(limits, channelSftp.put(limitUploadPathString)); //this uploads only partial xml file to sftp server
marshaller.marshal(limits, System.err)); //THIS WORKS CORRECTLY AND THE FULL XML IS PRINTED!        

channelSftp.disconnect();
channelSftp.exit();

请注意,这不可能是JAXB问题,因为它将在其他地方打印完整的XML,但是只有部分XML被上载到远程服务器。可能是什么问题?预先感谢!

java jaxb sftp jsch
1个回答
1
投票

总是确保在完成对OutputStream的写入之后,刷新/关闭它。

try(OutputSteam fileStream = channelSftp.put(limitUploadPathString)) {
  marshaller.marshal(limits, fileStream);
}
© www.soinside.com 2019 - 2024. All rights reserved.