2013-04-17 43 views
2

我正在研究Spring Web應用程序,在我的控制器之一中,我編寫了代碼來處理一些內容並寫出了放置Jasper報告。這段代碼工作正常,但有時會拋出上述異常。我正確關閉所有的輸出流,但仍然出現這個錯誤,任何想法我錯了。Response.getoutputstream已經爲此響應調用了異常,同時將輸出寫入碧玉報告

這裏是代碼,我在我的控制器

@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"; 
} 
} 

回答

2

我覺得例外是因爲,你寫的代碼只關閉內部try塊輸出流。但是如果在過程中出現任何異常,會發生什麼?所以你需要在你的catch塊中添加out.close();

+0

Thnx,你是對的! – HancockTheSmartGuy