在Java中使用JSCH传输文件时遇到问题。文件中的数据已损坏,并且这种情况是间歇性发生的。我的意思是有时文件正确上传,并且大多数时候我们注意到文件大小大于5 MB时数据已损坏。
该程序在不同情况下的行为有所不同。
Windows-10 :程序对于所有大小的文件均正常运行而没有问题。
Unix :对于小于2 MB的文件,该程序可以正常工作。但是对于大于2 MB的文件,有时文件可以正确上传,但是大多数情况下,我们会看到数据已损坏。
我仍然没有得到导致数据损坏的原因?我不认为代码有问题,因为程序在Windows环境中(有时在Unix环境中)也可以正常工作。
程序读取数据并将其写入远程服务器的方式是否有问题,或者我在这里缺少任何其他内容?请帮忙。
public boolean putFile(String report, String user, String password, String location,
String folder) throws Exception {
boolean status=true;
JSch shell = new JSch();
Session session = null;
session = shell.getSession(user, location, 22);
session.setPassword(password);
session.setConfig("StrictHostKeyChecking", "no");
session.connect();
Channel channel = null;
channel = session.openChannel("shell");
channel.setInputStream(null);
channel.setOutputStream(null);
channel = session.openChannel("sftp");
channel.connect();
ChannelSftp sftp = (ChannelSftp) channel;
sftp.cd(folder);
File outputFile = new File(report);
FileInputStream fileInputStream = new FileInputStream(outputFile);
sftp.put(fileInputStream, outputFile.getName());
session.disconnect();
return status;
}
答案 0 :(得分:0)
我们使用的jsch版本中存在一个错误。通读jsch版本的更改日志,并更新版本。那解决了问题。