java. Lang. IllegalStateException: stream when writing Excel files using jetty and struts as responses

I have a code to create Excel files using struts and jetty

In my declared struts In the XML file:

<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>

In my java code:

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());

I also have getters with content length:

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

The whole code runs under the dock When trying to download a large file, I get the following exception:

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)

Edit: it is suitable for smaller Excel files, but not for large files I also try to write large files to the file system (to ensure that it is not an excel export but a communication problem) and it is valid

Solution

Exceptions

Caused by: java.lang.IllegalStateException: STREAM
    at org.eclipse.jetty.server.Response.getWriter(Response.java:944)
    ...

... indicates that your code has accessed httpservletresponse After getoutputstream(), try to access httpservletresponse getWriter()

It's happening At the time point of the getwriter() call, the state of the response is already in stream mode, so the IllegalStateException occurs

According to the servlet specification, this is not allowed

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>