JSCH SFTP文件传输-文件中的数据已损坏



在java中使用JSCH传输文件时遇到问题。文件中的数据正在损坏,这种情况是间歇性发生的。我的意思是,有时文件上传得很好,大多数时候,当文件大小大于5 MB时,我们会注意到数据被损坏。

该程序在不同的场景中表现不同。

Windows-10:程序运行良好,对所有大小的文件都没有问题。

Unix:程序适用于小于2MB的文件。但对于大于2MB的文件,有时文件能够正确上传,但大多数时候我们会看到数据被损坏。

我仍然不知道是什么导致了数据损坏?我不认为代码有问题,因为程序在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;
}

我们使用的jsch版本中有一个错误。阅读jsch版本的更改日志,并更新版本。这解决了问题。

最新更新