java.io.IOException: Stream is closed

Spring JIRA | Tomoyuki Ikeya | 10 months ago
  1. 0

    We tried {{AbstractXlsxView}} as following implementation with Websphere Liberty Profile 8.5.5.8. Then we got {{IOException (stream is closed)}} at {{AbstractXlsView#renderWorkbook()}}. Of course, we confirmed the same implementation on Tomcat and it succeeded. {code:java} @Component public class ExcelView extends AbstractXlsxView { @Override protected void buildExcelDocument(Map<String, Object> model, Workbook workbook, HttpServletRequest request, HttpServletResponse response) throws Exception { Sheet sheet = workbook.createSheet("sheet1"); sheet.setFitToPage(true); String filename = "sample.xlsx"; response.setCharacterEncoding("UTF-8"); response.setHeader("Content-Disposition", "attachment;filename*=''" + URLEncoder.encode(filename, "UTF-8")); } } {code} We got following stacktrace. {noformat} [16/01/26 17:41:44:764 JST] 00000452 com.ibm.ws.webcontainer.util.ApplicationErrorUtils E SRVE0777E: アプリケーション・クラス 'com.ibm.ws.http.channel.internal.outbound.HttpOutputStreamImpl.validate:196' によって例外がスローされました java.io.IOException: Stream is closed at com.ibm.ws.http.channel.internal.outbound.HttpOutputStreamImpl.validate(HttpOutputStreamImpl.java:196) at com.ibm.ws.http.channel.internal.outbound.HttpOutputStreamImpl.flush(HttpOutputStreamImpl.java:571) at com.ibm.wsspi.http.ee7.HttpOutputStreamEE7.flush(HttpOutputStreamEE7.java:289) at com.ibm.ws.webcontainer.osgi.response.WCOutputStream.flush(WCOutputStream.java:234) at org.springframework.security.web.context.OnCommittedResponseWrapper$SaveContextServletOutputStream.flush(OnCommittedResponseWrapper.java:437) at org.springframework.web.servlet.view.document.AbstractXlsView.renderWorkbook(AbstractXlsView.java:101) at org.springframework.web.servlet.view.document.AbstractXlsView.renderMergedOutputModel(AbstractXlsView.java:74) at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:303) {noformat} By our investigation, the cause may be that {{AbstractXlsView}} (super class of {{AbstractXlsxView}}) calls {{outputStream#flush()}} twice. If we prevent one of them as following extension to disable flush, we never see the stacktrace and can download an expected excel file. In my opinion, it should be fixed to call once to support WebSphere Liberty Profile if there is no reason to flush twice. {code:java} @Override protected void renderWorkbook(Workbook workbook, HttpServletResponse response) throws IOException { ServletOutputStream out = response.getOutputStream(); workbook.write(out); //out.flush(); // Closeable only implemented as of POI 3.10 if (workbook instanceof Closeable) { ((Closeable) workbook).close(); } } {code}

    Spring JIRA | 10 months ago | Tomoyuki Ikeya
    java.io.IOException: Stream is closed
  2. 0

    We tried {{AbstractXlsxView}} as following implementation with Websphere Liberty Profile 8.5.5.8. Then we got {{IOException (stream is closed)}} at {{AbstractXlsView#renderWorkbook()}}. Of course, we confirmed the same implementation on Tomcat and it succeeded. {code:java} @Component public class ExcelView extends AbstractXlsxView { @Override protected void buildExcelDocument(Map<String, Object> model, Workbook workbook, HttpServletRequest request, HttpServletResponse response) throws Exception { Sheet sheet = workbook.createSheet("sheet1"); sheet.setFitToPage(true); String filename = "sample.xlsx"; response.setCharacterEncoding("UTF-8"); response.setHeader("Content-Disposition", "attachment;filename*=''" + URLEncoder.encode(filename, "UTF-8")); } } {code} We got following stacktrace. {noformat} [16/01/26 17:41:44:764 JST] 00000452 com.ibm.ws.webcontainer.util.ApplicationErrorUtils E SRVE0777E: アプリケーション・クラス 'com.ibm.ws.http.channel.internal.outbound.HttpOutputStreamImpl.validate:196' によって例外がスローされました java.io.IOException: Stream is closed at com.ibm.ws.http.channel.internal.outbound.HttpOutputStreamImpl.validate(HttpOutputStreamImpl.java:196) at com.ibm.ws.http.channel.internal.outbound.HttpOutputStreamImpl.flush(HttpOutputStreamImpl.java:571) at com.ibm.wsspi.http.ee7.HttpOutputStreamEE7.flush(HttpOutputStreamEE7.java:289) at com.ibm.ws.webcontainer.osgi.response.WCOutputStream.flush(WCOutputStream.java:234) at org.springframework.security.web.context.OnCommittedResponseWrapper$SaveContextServletOutputStream.flush(OnCommittedResponseWrapper.java:437) at org.springframework.web.servlet.view.document.AbstractXlsView.renderWorkbook(AbstractXlsView.java:101) at org.springframework.web.servlet.view.document.AbstractXlsView.renderMergedOutputModel(AbstractXlsView.java:74) at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:303) {noformat} By our investigation, the cause may be that {{AbstractXlsView}} (super class of {{AbstractXlsxView}}) calls {{outputStream#flush()}} twice. If we prevent one of them as following extension to disable flush, we never see the stacktrace and can download an expected excel file. In my opinion, it should be fixed to call once to support WebSphere Liberty Profile if there is no reason to flush twice. {code:java} @Override protected void renderWorkbook(Workbook workbook, HttpServletResponse response) throws IOException { ServletOutputStream out = response.getOutputStream(); workbook.write(out); //out.flush(); // Closeable only implemented as of POI 3.10 if (workbook instanceof Closeable) { ((Closeable) workbook).close(); } } {code}

    Spring JIRA | 10 months ago | Tomoyuki Ikeya
    java.io.IOException: Stream is closed
  3. Speed up your debug routine!

    Automated exception search integrated into your IDE

  4. 0

    "Output Stream is closed" exceptions in test console logs

    Eclipse Bugzilla | 5 years ago | Michael_Rennie
    java.io.IOException: Output Stream is closed
  5. 0

    NullPointerException when setting OnClickListener

    Stack Overflow | 3 years ago | NoobNinja
    java.io.IOException: Stream is closed

    Not finding the right solution?
    Take a tour to get the most out of Samebug.

    Tired of useless tips?

    Automated exception search integrated into your IDE

    Root Cause Analysis

    1. java.io.IOException

      Stream is closed

      at com.ibm.ws.http.channel.internal.outbound.HttpOutputStreamImpl.validate()
    2. com.ibm.ws
      HttpOutputStreamImpl.flush
      1. com.ibm.ws.http.channel.internal.outbound.HttpOutputStreamImpl.validate(HttpOutputStreamImpl.java:196)
      2. com.ibm.ws.http.channel.internal.outbound.HttpOutputStreamImpl.flush(HttpOutputStreamImpl.java:571)
      2 frames
    3. com.ibm.wsspi
      HttpOutputStreamEE7.flush
      1. com.ibm.wsspi.http.ee7.HttpOutputStreamEE7.flush(HttpOutputStreamEE7.java:289)
      1 frame
    4. com.ibm.ws
      WCOutputStream.flush
      1. com.ibm.ws.webcontainer.osgi.response.WCOutputStream.flush(WCOutputStream.java:234)
      1 frame
    5. Spring Security
      OnCommittedResponseWrapper$SaveContextServletOutputStream.flush
      1. org.springframework.security.web.context.OnCommittedResponseWrapper$SaveContextServletOutputStream.flush(OnCommittedResponseWrapper.java:437)
      1 frame
    6. Spring MVC
      AbstractView.render
      1. org.springframework.web.servlet.view.document.AbstractXlsView.renderWorkbook(AbstractXlsView.java:101)
      2. org.springframework.web.servlet.view.document.AbstractXlsView.renderMergedOutputModel(AbstractXlsView.java:74)
      3. org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:303)
      3 frames