title: Reading InputStream from rest API get call
body: <p>I am trying to call a rest controller using get call which reads files from file system and return InputStream of that file.</p>
  
  <p>client side code:</p>
  
  <pre><code>website = new URL(configurationFileUrl + "/stream/" + key.getReportRawDataId());
              URLConnection connection = website.openConnection();
              connection.setConnectTimeout(60 * 1000);
              connection.setReadTimeout(60 * 1000);
              stream = connection.getInputStream();
  </code></pre>
  
  <p>The controller code:</p>
  
  <pre><code>@RestController
  @RequestMapping(value = "/blueprintConfig", produces = {"application/octet-stream", "application/json"}, headers = "Accept=*/*")
  public class ReportConfigurationController {
  
  @ResponseBody
      @RequestMapping(value = "/stream/{id:.+}", method = RequestMethod.GET)
      public ResponseEntity&lt;?&gt; getFilesAsStream(@PathVariable("id") String id) throws Exception {
          return configurationService.getFilesAsStream(id);
      }
  
  }
  </code></pre>
  
  <p>service:</p>
  
  <pre><code>@Override
      public ResponseEntity&lt;?&gt; getFilesAsStream(String id) throws Exception {
          String filePath = resourcePath + id;
          InputStream is = null;
          try {
              is = new FileInputStream(filePath);
          } catch (FileNotFoundException e) {
              LOGGER.error("can not read file: {}", e);
          }
          return new ResponseEntity&lt;InputStream&gt;(is, HttpStatus.OK);
      }
  </code></pre>
  
  <p>while debugging I see the service is returning inputstream without any exception, but client side is throwing below IO exception:</p>
  
  <pre><code>java.io.IOException: Server returned HTTP response code: 406 for URL: http://localhost:8091/blueprintConfig/stream/1_check.png
  
  at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1839)
      at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1440)
      at com.jasper.wrapper.service.ReportServiceImpl.putInCache(ReportServiceImpl.java:128)
      at com.jasper.wrapper.service.ReportServiceImpl.populateCache(ReportServiceImpl.java:110)
      at com.jasper.wrapper.service.ReportServiceImpl.lambda$generateReport$0(ReportServiceImpl.java:64)
      at com.jasper.wrapper.service.ReportServiceImpl$$Lambda$8/819977316.accept(Unknown Source)
      at java.util.LinkedHashMap.forEach(LinkedHashMap.java:676)
      at com.jasper.wrapper.service.ReportServiceImpl.generateReport(ReportServiceImpl.java:64)
      at com.jasper.wrapper.controller.JasperReportController.generateReport(JasperReportController.java:27)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:497)
      at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
      at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
      at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
      at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:775)
      at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:705)
      at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
      at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
      at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
      at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:967)
      at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:869)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
      at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:843)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
      at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
      at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77)
      at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
      at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:85)
      at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:217)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
      at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
      at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
      at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:673)
      at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
      at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
      at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
      at java.lang.Thread.run(Thread.java:745)
  </code></pre>
  
  <p>what is wrong I am doing here I tried setting</p>
  
  <pre><code>connection.setRequestProperty("accept", "application/octet-stream");
  </code></pre>
  
  <p>but no luck, could you please help</p>