'Stream closed' exception occurs

2019-08-20 17:22发布

问题:

Recently i am developing a code to export data to excel. I used apache POI for developing this functionality.

Please consider the below code.

public static void prepareDateForCSVGeneration(List<DataList> dataList,HttpServletResponse response)
            throws CashBookingInquiryServiceApplicationException {

         try (ServletOutputStream out = response.getOutputStream()) {
              XSSFWorkbook workbook = new XSSFWorkbook();
              //below method creates headers and rows
              convertToExcel(workbook, dataList);
              response.setContentType("text/vnd.ms-excel");
                response.setHeader("Content-Disposition", "attachment;filename=test.xlsx");
                workbook.write(out);
                out.flush();
            } catch (IOException exception) {
                System.out.println("Exceptin in excel "+exception.getMessage());
                exception.printStackTrace() ;
            }
    }

everything works fine, means excel file downloads successfully but after that in console it throws exception :

Exceptin in excel Stream is closed
[err] java.io.IOException: Stream is closed
[err]     at com.ibm.ws.http.channel.internal.outbound.HttpOutputStreamImpl.validate(HttpOutputStreamImpl.java:213)
[err]     at [internal classes]

Not sure why this is happening.

i referred [java IO Exception: Stream Closed

but i did not find any concrete solution. Can you please suggest what to do? Thanks in advance.