Java.lang.IllegalStateException: STREAM while writing excel file as response with Jetty and struts

I have some code that creates an excel file using struts and jetty.

In the struts.xml file, I stated:

<action name="full-export-excel" method="exportFullDataSetToExcel"
  <result name="success" type="stream">
    <param name="contentType">application/vnd.openxmlformats-officedocument.spreadsheetml.sheet; charset=utf-8
    <param name="inputName">input</param>
    <param name="contentLength">${contentLength}</param>
    <param name="bufferSize">1024</param>
    <param name="contentDisposition">filename="${fileName}"</param>


In my java code:

final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
XSSFWorkbook excelWorkBook = excelBuilder.createExcelWorkBook(reportObjects, columnMapper); // My code for creating excel file
input = new ByteArrayInputStream(outputStream.toByteArray());


I also have a getter for content length:

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


All this code works under the dock. And when I try to upload a large file, I get this exception:

Caused by: java.lang.IllegalStateException: STREAM
    at org.eclipse.jetty.server.Response.getWriter(
    at org.eclipse.jetty.servlets.gzip.CompressedResponseWrapper.getWriter(
    at javax.servlet.ServletResponseWrapper.getWriter(


EDIT: It works for small excel files, but not for large ones. I also tried to write large files to the filesystem (to make sure not excel export but rather a communication problem) and it worked.


source to share

1 answer

An exception...

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


... means your code tried to access HttpServletResponse.getWriter()

after it already accessedHttpServletResponse.getOutputStream()

At the time the call was made .getWriter()

, the answer state was already in mode STREAM

, soIllegalStateException

This is not permitted by the servlet specification.



All Articles