title: In Java, how to write to a file from input stream? I am getting IOException: Pipe closed
body: <p>I created a file and writing an input stream to it but I am getting this exception with just this info <code>IOException: Pipe closed.</code></p>
  <p>Here is my code:</p>
  <pre><code>File outputFile = new File(filePath);
  
  try (OutputStream outputStream = new FileOutputStream(outputFile)) {
      IOUtils.copy(inputStream, outputStream); // exception here
  } catch (IOException e) {
      throw new FileWritingException(&quot;Exception while writing file::&quot; + e.getMessage());
  }
  </code></pre>
  <p>I also tried adding the following but still the same error. Only empty file gets created in that output location.</p>
  <pre><code>File file = new File(filePath);
  try (OutputStream outputStream = new FileOutputStream(file)) {
      IOUtils.copy(inputStream, outputStream);
      IOUtils.closeQuietly(outputStream);
  } catch (IOException e) {
      throw new FileWritingException(&quot;Exception while writing file::&quot; + e);
  } finally {
      IOUtils.closeQuietly(inputStream);
  }
  
  </code></pre>
  <p>FYI, I am getting input stream from implementing sftp java lib jsch as</p>
  <pre><code>ChannelSftp channelSftp = setupJsch();
  InputStream stream = channelSftp.get(&lt;pathtoFileInExternalFtPLocation&gt;);
  </code></pre>
  <pre><code>public ChannelSftp setupJsch() throws JSchException {
      JSch jsch = new JSch();
      jsch.setKnownHosts(&quot;known-hosts-path&quot;);
  
      Session jschSession = jsch.getSession(USERNAME, HOST, PORT);
      jschSession.setPassword(PASSWORD);
      jschSession.setProxy(new ProxyHTTP(SFTP_PROXY_HOST, SFTP_PROXY_PORT));
      Properties config = new Properties();
      config.put(STRICT_HOST_KEY_CHECKING_KEY, STRICT_HOST_KEY_CHECKING_VAL);
      jschSession.setConfig(config);
      jschSession.connect();
      return (ChannelSftp) jschSession.openChannel(AMLEFileConstants.SFTP);
    }
  </code></pre>
  <p>Jsch: <a href="https://epaul.github.io/jsch-documentation/javadoc/com/jcraft/jsch/ChannelSftp.html" rel="nofollow noreferrer">https://epaul.github.io/jsch-documentation/javadoc/com/jcraft/jsch/ChannelSftp.html</a></p>