今天做项目需要用导出数据到Excel,数据导出都正常就是后台报getOutputStream() has already been called for this response。
网上查了一下都说response.getOutputStream()和response.getWriter()这两个方法在一次请求中只能调一次,还有说
- out.clear();
- out = pageContext.pushBody();
这是在jsp中写输出容易犯的错误,我是在在action里的不存在这种情况。
每个方法都会返回一个ActionForward对象,而response是ActionForward对象参数,所以就会使response冲突,处理下载或上传的acton方法返回null就可以了
- @RequiresPermissions("account:aLoan:view")
@RequestMapping(value = "exportExcel", method = RequestMethod.POST)
public String exportListAloan(ALoan aLoan, HttpServletRequest request, HttpServletResponse response, Model model, RedirectAttributes redirectAttributes) {
exportListAloan(aLoan,request,response,model,redirectAttributes,"债权清单");
return null;//这里返回null解决 getOutputStream() has already been called for this response错误
}
public void exportListAloan(ALoan aLoan, HttpServletRequest request, HttpServletResponse response, Model model, RedirectAttributes redirectAttributes,String filename) {
Page<ALoan> page = new Page<ALoan>(request, response);
logger.debug("expExcel");
List<ALoan> list = aLoanService.findList(aLoan);
try {
String fileName = filename +"_" + DateUtils.getDate("yyyyMMddHHmmss") + ".xlsx";
new ExportExcel(filename, ALoan.class).setDataList(list).write(response, fileName).dispose();
} catch (IOException e) {
addMessage(redirectAttributes, "导出"+filename+"!失败信息:" + e.getMessage());
}
page.setGrossList(list);
model.addAttribute("page", page);
model.addAttribute("aLoan", aLoan);
}