我有一个使用struts和jetty创建excel文件的代码.
在我声明的struts.xml文件中:
<action name="full-export-excel" method="exportFullDataSetToExcel"
class="com.me.ExcelAction">
<result name="success" type="stream">
<param name="contentType">application/vnd.openxmlformats-officedocument.spreadsheetml.sheet; charset=utf-8
</param>
<param name="inputName">input</param>
<param name="contentLength">${contentLength}</param>
<param name="bufferSize">1024</param>
<param name="contentDisposition">filename="${fileName}"</param>
</result>
</action>
在我的java代码中:
final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
XSSFWorkbook excelWorkBook = excelBuilder.createExcelWorkBook(reportObjects, columnMapper); // My code for creating excel file
excelWorkBook.write(outputStream);
input = new ByteArrayInputStream(outputStream.toByteArray());
我也有内容长度的getter:
public Integer getContentLength() throws IOException {
return input.available();
}
整个代码在码头下运行.在尝试下载大文件时,我得到以下异常:
Caused by: java.lang.IllegalStateException: STREAM
at org.eclipse.jetty.server.Response.getWriter(Response.java:944)
at org.eclipse.jetty.servlets.gzip.CompressedResponseWrapper.getWriter(CompressedResponseWrapper.java:440)
at javax.servlet.ServletResponseWrapper.getWriter(ServletResponseWrapper.java:152)
编辑:它适用于较小的excel文件,但不适用于大型文件.我还尝试将大文件写入文件系统(以确保不是excel导出而是通信问题)并且它有效.
解决方法:
例外……
Caused by: java.lang.IllegalStateException: STREAM
at org.eclipse.jetty.server.Response.getWriter(Response.java:944)
...
…表示您的代码在访问过HttpServletResponse.getOutputStream()
后尝试访问HttpServletResponse.getWriter()
在发生.getWriter()调用的时间点,响应的状态已经处于STREAM模式,因此出现IllegalStateException
根据servlet规范,这是不允许的.