title: SFTP: IOException while reading a file with java
body: <p>I am using com.jcraft.jsch library to read .xls files from an SFTP server. Following is the code to connect to server.</p>
  
  <pre><code>    session = jsch.getSession(username, host);
      session.setConfig("StrictHostKeyChecking", "no");
      session.setPassword(password);
      session.connect();
      sftpChannel = (ChannelSftp) session.openChannel("sftp");
      sftpChannel.connect();
  </code></pre>
  
  <p>I am using <code>sftpChannel.get(file)</code> to retrieve inputStream to the file. This inputstream is then used to instantiate <code>XSSFWorkbook</code> as shown below:</p>
  
  <pre><code>XSSFWorkbook workbook = new XSSFWorkbook(in);
  </code></pre>
  
  <p><em>Problem 1</em>:</p>
  
  <p>When I run the app, it seems to get stuck on the above line for some time (say 5 minutes) and then it throws <code>java.io.IOException: Pipe closed</code> error.
  The xls file I am trying to read is 800kb and it works fine when run from local machine.</p>
  
  <p><em>Problem 2</em>:</p>
  
  <p>The app is designed to process files sequentially. So, if first file fails with IOE, rest of the files also fail as the connection is timed out. To prevent this, I put the below code to check and re-connect:</p>
  
  <pre><code>if(null == session || !session.isConnected()){
          log.debug("Session is not connected/timed out. Creating a new session");
          openSftpSession();
          log.debug("New session is created");
      }
  //openSftpSession() is the code to create a new session as explained in the beginning of the question.
  </code></pre>
  
  <p>When this code gets executed, following exception gets thrown:</p>
  
  <pre><code>java.io.IOException: error: 4: RequestQueue: unknown request id 1028332337
      at com.jcraft.jsch.ChannelSftp$2.read(ChannelSftp.java:1407)
      at java.io.FilterInputStream.read(FilterInputStream.java:133)
      at java.io.PushbackInputStream.read(PushbackInputStream.java:186)
  //More lines
  </code></pre>
  
  <p><strong><em>edit</em></strong> : code to retrieve input stream</p>
  
  <pre><code>public InputStream getInputStream(String folder, String file) throws Exception{
      sftpChannel.cd(root + folder);
      log.debug("current directory:" + sftpChannel.pwd());
      log.debug("File :" + folder + " " + file);
      return sftpChannel.get(file);
  }
  </code></pre>
  
  <p>Can anyone please help me get over this? I believe an alternate approach to prevent timeout is to download the file in some temp directory and process. However, I don't really want to do that.</p>
  
  <p>Thanks in advance.</p>