public void downloadFailedUsingJson(HttpServletResponse response) throws IOException { try {
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setCharacterEncoding("utf-8");
// 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
String fileName = URLEncoder.encode("测试", "UTF-8").replaceAll("\\+", "%20");
response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
// 这里需要设置不关闭流
EasyExcel.write(response.getOutputStream(), DownloadData.class).autoCloseStream(Boolean.FALSE).sheet("模板")
.doWrite(data());
} catch (Exception e) {
// 重置response
response.reset();
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
Map<String, String> map = new HashMap<String, String>();
map.put("status", "failure");
map.put("message", "下载文件失败" + e.getMessage());
response.getWriter().println(JSON.toJSONString(map));
}
}
用的这个官方文档代码。
测试工具:postman
问题:我使用本地代码下载几千条数据没问题可以下载成功,但是我把项目部署导服务器上面的时候下载几百条没问题但是千条以上日志就会报:系统异常java.lang.illegalstateException:Cannot call reset() after response has been committed, org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation,下载的response.json 文件内就是:system is not startup!