gpt4 book ai didi

java.lang.IllegalStateException : STREAM when writing excel file as response with Jetty and struts 错误

转载 作者:搜寻专家 更新时间:2023-11-01 04:04:14 25 4
gpt4 key购买 nike

我有一个使用 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());

我还有一个获取内容长度的方法:

public Integer getContentLength() throws IOException {
return input.available();
}

整个代码都在 jetty 下运行。尝试下载大文件时出现此异常:

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.getWriter()在已经访问 HttpServletResponse.getOutputStream() 之后

.getWriter() 调用发生的时间点,响应的状态已经处于 STREAM 模式,因此 IllegalStateException

根据 servlet 规范,这是不允许的。

关于java.lang.IllegalStateException : STREAM when writing excel file as response with Jetty and struts 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25890991/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com