title: Streaming excel file using Jersey output stream
body: <p>I'm using apache-poi to build an excel file and return it to the client via Jersey output stream.</p>
  
  <p>The code looks like this:</p>
  
  <pre><code>final SXSSFWorkbook workbook = ExcelFormatter.buildWorkbook();
  
          StreamingOutput stream = new StreamingOutput() {
              @Override
              public void write(OutputStream output) throws IOException, WebApplicationException {
                  try {
                      workbook.write(output);
                      workbook.close();
                      workbook.dispose();
                  } catch (Exception e) {
                      throw new WebApplicationException(e);
                  }
              }
          };
  
          return Response.ok(stream).header("content-disposition","attachment; filename = export.xlsx").type("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet").build();
  </code></pre>
  
  <p>It works just fine with smaller datasets up to about 5000 records, however, when I attempt to generate a file with 20k + records (around 5mb), I get the below error at workbook.write(output):</p>
  
  <blockquote>
    <p>org.glassfish.jersey.server.ServerRuntime$Responder.writeResponse An
    I/O error has occurred while writing a response message entity to the
    container output stream.  javax.ws.rs.WebApplicationException: HTTP
    500 Internal Server Error</p>
    
    <p>Caused by: org.apache.catalina.connector.ClientAbortException:
    java.io.IOException: An established connection was aborted by the
    software in your host machine     at
    org.apache.catalina.connector.OutputBuffer.realWriteBytes(OutputBuffer.java:393)</p>
  </blockquote>
  
  <p>It's worth noting that I can write the file to disk using FileOutputStream regardless of the workbook size;</p>
  
  <pre><code>try {
           FileOutputStream fos = new FileOutputStream(File);
              workbook.write(fos);
              fos.close();
          } catch (FileNotFoundException e) {
              e.printStackTrace();
          } catch (IOException e) {
              e.printStackTrace();
          }
  </code></pre>
  
  <p>Has anyone here experienced similar issues with Jersey / apache-poi? Any suggestions for a fix or a suitable workaround very much welcomed.</p>