My file transfer to SFTP server is failing with exception: java.io.IOException: inputstream is closed
- My code has been working for the last 1 year or so, without any issues.
- Nor I did any code change.
- This has been happening since last three days.
I tried to check if there was any changes to the Library in the last few days and there have been none, also I'm using the latest one : jsch-0.1.54.jar
Here is my code:
public void sendVsbFile(String fileName, MyObject c) {
File f = new File(fileName);
if (f.exists()) {
String SFTPHOST = c.SFTPURL;
int SFTPPORT = Integer.parseInt(c.SFTPPORT);
String SFTPUSER = c.SFTPUserName;
String SFTPPASS = c.SFTPPassword;
String SFTPWORKINGDIR = c.FTPFolderLocation;
Session session;
Channel channel;
ChannelSftp channelSftp;
try {
JSch jsch = new JSch();
session = jsch.getSession(SFTPUSER, SFTPHOST, SFTPPORT);
session.setPassword(SFTPPASS);
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
config.put("TCPKeepAlive", "yes");
session.setConfig(config);
session.setServerAliveInterval(120 * 1000);
session.setServerAliveCountMax(1000);
session.setTimeout(timeout);
session.connect();
System.out.println("Host connected.");
channel = session.openChannel("sftp");
channelSftp = (ChannelSftp) channel;
channelSftp.connect();
System.out.println("sftp channel opened and connected.");
channelSftp.cd(SFTPWORKINGDIR);
channelSftp.cd(Environment.getExternalStorageDirectory().getAbsolutePath() + "/MyLocalAndroidPath/");
String suffix = ".filepart";
Log.d(TAG, "file name = " + f.getName());
String tempFileName = f.getName() + suffix;
channelSftp.put(new FileInputStream(f), tempFileName, ChannelSftp.RESUME);
String newFileName = tempFileName.substring(0, tempFileName.length() - suffix.length());
channelSftp.rename(tempFileName, newFileName);
System.out.println("File transferred successfully to host.");
} catch (JSchException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (SftpException e) {
e.printStackTrace();
}
}
}
Many developers have posted this kind of issue, but there's no concrete solution provided. Please if anyone has ever faced this issue before, do help. Thanks.
Here is the Stack Trace:
java.io.IOException: inputstream is closed
at com.jcraft.jsch.ChannelSftp._put(ChannelSftp.java:697)
at com.jcraft.jsch.ChannelSftp.put(ChannelSftp.java:475)
at com.jcraft.jsch.ChannelSftp.put(ChannelSftp.java:365)
at myMethod(MyClass.java:933)
at myMethod.run(MyClass.java:804)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.io.IOException: inputstream is closed
at com.jcraft.jsch.ChannelSftp.fill(ChannelSftp.java:2911)
at com.jcraft.jsch.ChannelSftp.header(ChannelSftp.java:2935)
at com.jcraft.jsch.ChannelSftp.checkStatus(ChannelSftp.java:2473)
at com.jcraft.jsch.ChannelSftp._put(ChannelSftp.java:686)
... 5 more