Jersey 将大型HSSF工作簿写入ServletOutputStream
我有下面的代码。当我尝试将记录列表导出到包含少量记录(约200条记录)的excel时,这种方法效果很好: 当列表的大小变大时(ex超过200条记录),悲剧就开始了。它给出了以下错误:Jersey 将大型HSSF工作簿写入ServletOutputStream,jersey,jax-rs,hssfworkbook,Jersey,Jax Rs,Hssfworkbook,我有下面的代码。当我尝试将记录列表导出到包含少量记录(约200条记录)的excel时,这种方法效果很好: 当列表的大小变大时(ex超过200条记录),悲剧就开始了。它给出了以下错误: <An I/O error has occurred while writing a response message entity to the container output stream. org.glassfish.jersey.server.internal.process.MappableEx
<An I/O error has occurred while writing a response message entity to the container output stream.
org.glassfish.jersey.server.internal.process.MappableException: java.io.IOException: CLOSED
冲洗小溪是大多数人所指的唯一方法。现在,这个解决方案不仅适用于大数据,也适用于小数据(不适用于两种情况)。它给出了以下错误:
An I/O error has occurred while writing a response message entity to the container output stream.
org.glassfish.jersey.server.internal.process.MappableException: com.fasterxml.jackson.databind.JsonMappingException: Direct self-reference leading to cycle
我尝试了XSSFWorkbook
而不是HSSFWorkbook
。但是weblogic甚至没有构建该项目,并且抛出了一个与元模型相关的错误
不管怎样,这个代码有什么问题。如何将较大的文件写入流?我的apache poi版本是3.11。正如Paul Samsotha
评论的S.O link所说,我应该使用StreamingOutput
而不是ServletOutputStream
。因此,该方法应重写为:
@Path("/toExcel")
@GET
public Response excelCustomersReport(@QueryParam("page") Integer page, @QueryParam("start") Integer start, @QueryParam("limit") Integer limit, @QueryParam("filter") FilterWrapper filter, @QueryParam("sort") SortWrapper sort) throws Exception {
response.setContentType("application/octet-stream");
DateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss");
String currentDateTime = dateFormatter.format(new Date());
String headerKey = "Content-Disposition";
String headerValue = "attachment; filename=report_" + currentDateTime + ".xlsx";
response.setHeader(headerKey, headerValue);
HSSFWorkbook workbook=userService.export(page,start,limit,filter,sort);
StreamingOutput output = new StreamingOutput() {
@Override
public void write(OutputStream out)
throws IOException, WebApplicationException {
workbook.write(out);
out.flush();
}
};
return Response.ok(output)
.build();
}
tnx!解决了这个问题!
An I/O error has occurred while writing a response message entity to the container output stream.
org.glassfish.jersey.server.internal.process.MappableException: com.fasterxml.jackson.databind.JsonMappingException: Direct self-reference leading to cycle
@Path("/toExcel")
@GET
public Response excelCustomersReport(@QueryParam("page") Integer page, @QueryParam("start") Integer start, @QueryParam("limit") Integer limit, @QueryParam("filter") FilterWrapper filter, @QueryParam("sort") SortWrapper sort) throws Exception {
response.setContentType("application/octet-stream");
DateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss");
String currentDateTime = dateFormatter.format(new Date());
String headerKey = "Content-Disposition";
String headerValue = "attachment; filename=report_" + currentDateTime + ".xlsx";
response.setHeader(headerKey, headerValue);
HSSFWorkbook workbook=userService.export(page,start,limit,filter,sort);
StreamingOutput output = new StreamingOutput() {
@Override
public void write(OutputStream out)
throws IOException, WebApplicationException {
workbook.write(out);
out.flush();
}
};
return Response.ok(output)
.build();
}