Cannot call sendError() after the response has been committed
程序员文章站
2022-03-29 17:54:29
...
在一个springboot项目中,使用Apache的POI框架导出Excel表格时,遇到这个问题,百度了下,大概意思是response已经提交,无法发送错误信息。Excel表格能正常导出,只是控制台一直输出这个错误,于是想办法改正。检查controller和service层,都没有发现问题,后来去导出表格的工具方法中检查。
public void exportExcel(HttpServletResponse response, List<T> list, String[] header) throws Exception {
Date dt = new Date();
String fileName = new Long(dt.getTime()).toString();
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xls");
// File file =new File("E:\\"+fileName+".xls");
OutputStream out = response.getOutputStream();
// OutputStream out =new FileOutputStream(file);
try {
exportExcel(header, list, out, "yyyy-MM-dd");
out.flush();
} catch (Exception e) {
throw e;
} finally {
out.close();
}
}
发现这几句代码有点问题
OutputStream out = response.getOutputStream();
try {
...
} catch (Exception e) {
throw e;
} finally {
out.close();
}
我在controller的方法上加了@ResponseBody的注解,而且导出excel方法返回了Json,但是在导出excel的工具类方法中已经将response生产的OutputStream关闭,但是@ResponseBody注解使用的就是response的OutputStream输出的方法,所以此处冲突报错。
解决的方法就是将Controller层导出的方法返回类型改为void
上一篇: 客户订阅RSS,定时更新的方法
下一篇: 文章标题
推荐阅读
-
java.lang.IllegalStateException: Cannot call sendError() after the response has been committed解读
-
java.lang.IllegalStateException: Cannot forward after response has been committed
-
Cannot call reset() after response has been committed
-
Hibernate项目报错:Cannot call sendError() after the response has been committed
-
java.lang.IllegalStateException: Cannot call sendError() after the response has been committed解读
-
Cannot forward after response has been committed问题分析及解决
-
Hibernate项目报错:Cannot call sendError() after the response has been committed
-
Cannot call sendError() after the response has been committed 错误记录
-
Cannot call sendError() after the response has been committed
-
错误提示:Cannot call sendError() after the response has been committed的解决方法