Easyexcel异常处理:getOutputStream() has already been called for this response
生活随笔
收集整理的這篇文章主要介紹了
Easyexcel异常处理:getOutputStream() has already been called for this response
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 異常日志
- 源碼位置
- 異常原因
- 異常前代碼
- 調整后代碼
異常日志
java.lang.IllegalStateException: getOutputStream() has already been called for this responseat org.apache.catalina.connector.Response.getWriter(Response.java:582) ~[tomcat-embed-core-9.0.17.jar!/:9.0.17]at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:227) ~[tomcat-embed-core-9.0.17.jar!/:9.0.17]at javax.servlet.ServletResponseWrapper.getWriter(ServletResponseWrapper.java:152) ~[javax.servlet-api-3.1.0.jar!/:3.1.0]源碼位置
看起來是,output流使用標志生效導致異常。
異常原因
一句話總結:同一個請求中:調用了兩次response.getOutputStream()方法。或者先進行了respose的頭等信息設置后調用response.getOutputStream()
異常前代碼
控制層:
@RequestMapping(value ="/detailExport" ,method = RequestMethod.POST)@BusiResponseBodypublic void detailExport(@RequestBody IdReqBO queryBO, HttpServletRequest request,HttpServletResponse response ) {//獲取導出的元數據(轉換器、格式控制器、導出內容等)ExcelExportContent excelExportContent = excelExportStrategy.exportDetailExcelContent(request.getRequestURI(), queryBO);EasyExcelUtils.ExportResponse(excelExportContent,response);}工具類:
public class EasyExcelUtils {public static void ExportResponse(ExcelExportContent excelExportContent,HttpServletResponse response){try {response.setContentType("application/vnd.ms-excel");response.setCharacterEncoding("utf-8");String fileName = URLEncoder.encode(excelExportContent.getFileName(), "UTF-8").replaceAll("\\+", "%20");response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");//這里先設置的response的頭、編碼等信息,導致response.getOutputStream()時異常ExcelWriterBuilder excelWriterBuilder = EasyExcel.write(response.getOutputStream(), excelExportContent.getAccountDataClass()).registerConverter(new BigDecimalStringConverter()).registerConverter(new DateStringConverter());List<WriteHandler> writeHandler = excelExportContent.getWriteHandler();for (WriteHandler handler : writeHandler) {excelWriterBuilder.registerWriteHandler(handler);}excelWriterBuilder.sheet(excelExportContent.getSheetName()).doWrite(excelExportContent.getAccountData());} catch (Exception e){e.printStackTrace();throw new ZTBusinessException(e.getMessage());}} }調整后代碼
控制層不變:
@RequestMapping(value ="/detailExport" ,method = RequestMethod.POST)@BusiResponseBodypublic void detailExport(@RequestBody IdReqBO queryBO, HttpServletRequest request,HttpServletResponse response ) {//獲取導出的元數據(轉換器、格式控制器、導出內容等),可忽略ExcelExportContent excelExportContent = excelExportStrategy.exportDetailExcelContent(request.getRequestURI(), queryBO);EasyExcelUtils.ExportResponse(excelExportContent,response);}工具類:
這里格式化response的動作只能在response.getOutputStream()之后,在EasyExcel的doWrite之前。
在response.getOutputStream()之前會出現getOutputStream() has already been called for this response異常。在EasyExcel的doWrite之后,流已經被寫回了,格式化response不會生效,寫回的內容是txt格式。
總結
以上是生活随笔為你收集整理的Easyexcel异常处理:getOutputStream() has already been called for this response的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MyBatis-Plus_查询进阶05
- 下一篇: 忽略特殊文件