错误提示:Cannot call sendError() after the response has been committed的解决方法
程序员文章站
2022-03-29 17:51:22
...
之前在开发接口的时候碰到这样一个问题,程序报了这样一个错误:Cannot call sendError() after the response has been committed。
这个问题是怎么产生的呢?
我的接口作用主要是返回一个文件的文件流,我把文件流存放在响应头response,最后还用return返回某个对象,导致出现这样的问题。
出现问题的代码块:
File file = new File(fileDir);
if (file.exists()) {
InputStream inputStream = null;
OutputStream outputStream = null;
try {
response.reset();
response.addHeader("Content-Length", "" + file.length());
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "inline;filename = " + fileName);
outputStream = new BufferedOutputStream(response.getOutputStream());
inputStream = new BufferedInputStream(new FileInputStream(file));
byte[] buffer = new byte[inputStream.available()];
inputStream.read(buffer);
outputStream.write(buffer);
} catch (Exception e) {
e.printStackTrace();
String message = "获取文件失败";
return JsonResult.failure(message);
} finally {
inputStream.close();
outputStream.flush();
outputStream.close();
}
return JsonResult.success(message); // 这句代码将导致出现问题
}
解决方法:只要将最后的return JsonResult.success(message);改为return null;
这样就可以完美解决问题啦!
博文参考:https://blog.csdn.net/u012377333/article/details/72263686
上一篇: Vue对象支持扩展运算符
推荐阅读
-
java.lang.IllegalStateException: Cannot call sendError() after the 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解读
-
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的解决方法
-
Cannot call reset() after response has been committed