title: Why is the InputStreamResource closed when I start reading from it?
body: <p>I have a MongoDB, which stores a csv file, and two microservices (Spring Boot apps). One microservice handles file uploads and supports to get those files as a stream. The other microservice processes files. Those microservices are separated, because I maybe want to store files later in another way than in a MongoDB and I don't want to effect other services by this.</p>
  
  <p>My problem is that when the file service returns the file as a InputStreamResource like this:</p>
  
  <pre><code>@RequestMapping(value = "/upload/{importId}", method = RequestMethod.GET)
  @ResponseBody
  public ResponseEntity&lt;InputStreamResource&gt; getUploadedFile(@PathVariable("importId") String importId)
          throws ImportFileNotFoundException {
      GridFSDBFile file = FileImportClient.getInstance().getFile(importId);
  
      return ResponseEntity
              .ok()
              .contentLength(file.getLength())
              .body(new InputStreamResource(file.getInputStream()));
  }
  </code></pre>
  
  <p>And I want to read from the stream it is already closed.</p>
  
  <pre><code>ResponseEntity&lt;InputStreamResource&gt; response = restTemplate
              .getForEntity("http://upload-service/upload/" + importId, InputStreamResource.class);
  BufferedReader fileReader = new BufferedReader(new InputStreamReader(response.getBody().getInputStream()));
  for (CSVRecord record : CSVFormat.DEFAULT.parse(fileReader)) {/*toStuff()*/}
  </code></pre>
  
  <p>And I am getting a <code>java.io.IOException: Stream closed</code> caused by the parse method.</p>
  
  <p>If I just <code>curl localhost:&lt;port&gt;/upload/&lt;importId&gt;</code> I get the whole csv file.
  Does anyone know, why the InputStream is closed and how to fix that?</p>
  
  <p>Stack trace:</p>
  
  <pre><code>java.io.IOException: Stream closed
  at java.io.BufferedInputStream.getBufIfOpen(BufferedInputStream.java:170)
  at java.io.BufferedInputStream.read(BufferedInputStream.java:336)
  at java.io.FilterInputStream.read(FilterInputStream.java:133)
  at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284)
  at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326)
  at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
  at java.io.InputStreamReader.read(InputStreamReader.java:184)
  at java.io.BufferedReader.read1(BufferedReader.java:210)
  at java.io.BufferedReader.read(BufferedReader.java:286)
  at java.io.BufferedReader.fill(BufferedReader.java:161)
  at java.io.BufferedReader.read(BufferedReader.java:182)
  at org.apache.commons.csv.ExtendedBufferedReader.read(ExtendedBufferedReader.java:60)
  at org.apache.commons.csv.Lexer.nextToken(Lexer.java:89)
  at org.apache.commons.csv.CSVParser.nextRecord(CSVParser.java:498)
  at org.apache.commons.csv.CSVParser$1.getNextRecord(CSVParser.java:439)
  at org.apache.commons.csv.CSVParser$1.hasNext(CSVParser.java:452)
  at &lt;mypackage&gt;.&lt;myclass&gt;.process(&lt;myclass&gt;.java:71)
  </code></pre>