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.