在将输出写入jasper报告时,Response.getoutputstream已调用此响应异常

时间:2013-04-17 06:24:12

标签: web-applications spring-mvc jasper-reports

我正在使用spring web app,在我的一个控制器中,我编写了代码来处理一些东西并写出put jasper报告。这段代码工作正常,但有时它会抛出异常。我正在关闭所有输出流但仍然得到这个错误,任何想法我错了。?

这是我控制器中的代码

@RequestMapping(method = { RequestMethod.GET, RequestMethod.POST }, value = "/export.do")
public String display(ModelMap model, HttpServletRequest request,   HttpServletResponse response,@RequestParam(required = false, value = "type") String type,
@RequestParam(required = false, value = "jrxml") String jrxml) throws IOException {
  Map imagesMap = new HashMap();
  String sum=request.getParameter("typ");   
  request.getSession().setAttribute("IMAGES_MAP", imagesMap);
  SearchCriteria criteria = (SearchCriteria) request.getSession() .getAttribute("searchCriteria");
  criteria.setPageSize(500000);
  criteria.setPage(0);
  BillingHistoryInputinput=BillingHistoryInput)request.getSession().getAttribute("input");
  ByteArrayOutputStream out = new ByteArrayOutputStream();
  try {
   Map<String,Object> datas = generateData(criteria, request, input);
   if (StringUtils.isEmpty(type))
    type = "xlsx";
   if (!type.equals("html") && !(type.equals("print"))) {
    response.setHeader("Content-Disposition","attachment;filename= billinghistory." + type);
   }
   response.setContentType(MimeUtil.getContentType(type));          
   Map<String, Object> params = (Map<String,Object>)datas.get("params");
   if (!type.equals("print")&&!type.equals("pdf")) {
    out = dynamicReportService.generateStaticReport("billinghistory",
    (List)datas.get("data"), params, type, request);
   }
   else if (type.equals("pdf")) {
    out = dynamicReportService.generateStaticReport("billinghistorypdf",
    (List)datas.get("data"), params, type, request);
   }
   else {
    out = dynamicReportService.generateStaticReport("billinghistory"+"print",      (List)datas.get("data"), params, type, request);
   }
   out.writeTo(response.getOutputStream());
   criteria.setPageSize(500);
   out.flush();
   out.close();
   return null;
} 
catch (Exception e) {
  e.printStackTrace();
  log.warn("Unable to create file :" + e);
  request.getSession().setAttribute("errors", e.getMessage());
  return "error";
 }
} 

1 个答案:

答案 0 :(得分:2)

我认为异常是因为,您编写的代码仅在try块内关闭输出流。但是,如果在进程中间抛出任何异常,会发生什么?所以你需要在catch块中添加out.close();