title: Spring RestTemplate streaming response into another request
body: <p>I am trying to stream the result of a file download directly into another post using spring's <code>RestTemplate</code></p>
  
  <p>My current approach is the following:</p>
  
  <pre><code>   ResponseEntity&lt;InputStreamResource&gt; downloadResponse = restTemplate.getForEntity(fileToDownloadUri, InputStreamResource.class);
  
     InputStreamResource imageInputStreamResource = downloadResponse.getBody();
  
     ResponseEntity&lt;String&gt; response = restTemplate.exchange(storageUri, POST, new HttpEntity&lt;&gt;(imageInputStreamResource), String.class);
  </code></pre>
  
  <p>However, I get the following exception running the code above:</p>
  
  <pre><code>org.springframework.web.client.ResourceAccessException: I/O error on POST request for "http://host:port/path/some.jpg": stream is closed; nested exception is java.io.IOException: stream is closed
  
      at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:6
  ...
  Caused by: java.io.IOException: stream is closed
      at sun.net.www.protocol.http.HttpURLConnection$HttpInputStream.ensureOpen(HttpURLConnection.java:3348)
      at sun.net.www.protocol.http.HttpURLConnection$HttpInputStream.read(HttpURLConnection.java:3373)
  </code></pre>
  
  <p>It seems that the response is always closed as the final step of processing. With the response, the <code>HttpURLConnection</code> is closed, and the stream is no longer processable.</p>
  
  <p>I would like to be able to implement this scenario without having to hold the file completely in memory or writing it to a file (as described <a href="https://stackoverflow.com/a/15785322/5371736">here</a>).</p>
  
  <p>Any hints are highly appreciated.</p>