由于对spring框架不熟悉,导致了此错误,记录一下,如理解有误,谢谢各位大佬指正。
用springboot搭建了项目,需要导出excel,在调用的时候出现了此错误:
java.lang.IllegalStateException: getOutputStream() has already been called for this response
究其原因是,因为写文件的时候调用了getOutputStream()方法,如果接口还返回了其它数据,这样会造成2次调用getOutputStream(),所以报了此错误;
报错时的写法(有返回数据时,spring框架会自动调用一次getOutputStream()):
@RequestMapping("download")
@ResponseBody
public String download(HttpServletResponse request){
//download op
..
return "data";
}
修改后的写法(没有返回数据):
@RequestMapping("download")
@ResponseBody
public void download(HttpServletResponse request){
//download op
..
}