【发布时间】:2018-08-01 04:34:19
【问题描述】:
我正在使用 apache-poi 构建一个 excel 文件并通过 Jersey 输出流将其返回给客户端。
代码如下所示:
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();
它适用于最多约 5000 条记录的较小数据集,但是,当我尝试生成包含 20k + 条记录(约 5mb)的文件时,我在 workbook.write(output) 处收到以下错误:
org.glassfish.jersey.server.ServerRuntime$Responder.writeResponse An 将响应消息实体写入 容器输出流。 javax.ws.rs.WebApplicationException:HTTP 500 内部服务器错误
引起:org.apache.catalina.connector.ClientAbortException: java.io.IOException:已建立的连接被 主机中的软件 org.apache.catalina.connector.OutputBuffer.realWriteBytes(OutputBuffer.java:393)
值得注意的是,无论工作簿大小如何,我都可以使用 FileOutputStream 将文件写入磁盘;
try {
FileOutputStream fos = new FileOutputStream(File);
workbook.write(fos);
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
这里有人遇到过类似的 Jersey / apache-poi 问题吗?非常欢迎任何有关修复或合适解决方法的建议。
【问题讨论】:
标签: java jersey apache-poi