I am facing problem while transferring the file using JSCH in java. The data in the file is getting corrupted, and this is happening intermittently . I mean sometimes the file is getting uploaded properly and majority times we noticed data is being corrupted when the file size is greater than 5 MB.
The program is behaving differently in different scenarios.
Windows-10 : Program works perfectly fine without issues for all sizes of files.
Unix : Program works fine for files less than 2 MB. But for files that are greater than 2 MB, sometimes the file is able to upload correctly, but majority times we see the data being corrupted.
I am still not getting what is causing the data corruption ? I don't think code has the issue as program works fine in windows environment, and sometimes in unix environment as well.
Is there any problem with the way the program reads the data and writes to remote server or any other thing I am missing here? Please help.
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;
}
There was a bug in the jsch version we were using. Read through the change log of jsch releases, and updated the version. That solved the problem.