0

I'm using apache-poi to build an excel file and return it to the client via Jersey output stream.

The code looks like this:

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();

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):

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

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)

It's worth noting that I can write the file to disk using FileOutputStream regardless of the workbook size;

try {
         FileOutputStream fos = new FileOutputStream(File);
            workbook.write(fos);
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

Has anyone here experienced similar issues with Jersey / apache-poi? Any suggestions for a fix or a suitable workaround very much welcomed.

shanahobo86
  • 467
  • 2
  • 7
  • 23

2 Answers2

1

There's probably nothing wrong with your server code, and when I've seen this it's a client hanging up due to a timeout somewhere in the network process.

Broken Pipe when writing bytes in ServletOutputStream

mikeb
  • 10,578
  • 7
  • 62
  • 120
  • Thanks for the response but I haven't seen any error that suggests a broken pipe or a client connection time out. It specifically happens when attempting workbook.write(output). It's annoying! – shanahobo86 Feb 21 '18 at 15:35
  • OK, so try pausing for 30 seconds or something then writing the shorter workbook – mikeb Feb 21 '18 at 19:48
0

I upgraded my apache-poi jars to 3.8 which seems to have fixed the issue

shanahobo86
  • 467
  • 2
  • 7
  • 23
  • "upgraded my apache-poi jars to 3.8": Don't call this a upgrade.[Version 3.8-FINAL (2012-03-26)](https://poi.apache.org/changes.html#3.8-FINAL) is nearly **6** years old now. Why people are using such ancient versions? – Axel Richter Feb 22 '18 at 07:52
  • You're right, I downgraded! 3.17 is the newest version. Is it just me or does 3.8 seem like a more recent version numerically than 3.17? – shanahobo86 Feb 22 '18 at 09:32