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"
        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 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(Response.java:944)
    at org.eclipse.jetty.servlets.gzip.CompressedResponseWrapper.getWriter(CompressedResponseWrapper.java:440)
    at javax.servlet.ServletResponseWrapper.getWriter(ServletResponseWrapper.java:152)

      

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.

+3


source to share


1 answer


An exception...

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

      

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

+13


source







All Articles