2

我正在开发 Spring Web 应用程序,在我的一个控制器中,我编写了代码来处理一些东西并写出 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";
 }
} 
4

1 回答 1

2

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

于 2013-04-17T06:32:17.657 回答