0

My file transfer to SFTP server is failing with exception: java.io.IOException: inputstream is closed

  1. My code has been working for the last 1 year or so, without any issues.
  2. Nor I did any code change.
  3. 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
  • Show us exception callstack. Show us references to the *"Many developers have posted this kind of issue"*. – Martin Prikryl Oct 06 '17 at 15:55
  • Hey Martin, I have edited my question with the exception stacktrace. For similar question posted, please check the below links : https://stackoverflow.com/questions/33405888/inputstream-is-closed-error-while-uploading-zip-file-through-jsch-to-sftp-site & https://stackoverflow.com/questions/8968611/why-do-i-get-java-io-ioexception-stream-closed – Mukesh Sharma Oct 09 '17 at 06:12
  • OK, anything useful in JSch log file? – Martin Prikryl Oct 09 '17 at 06:15
  • No idea. Where would I find the JSch Logs? Do I need to implement the JSch Logger for that? – Mukesh Sharma Oct 09 '17 at 09:00
  • Yes, see http://www.jcraft.com/jsch/examples/Logger.java.html – Martin Prikryl Oct 09 '17 at 09:01

0 Answers0