導出Excel出現getOutputStream() has already been called for this response異常的原因

下jsp中出現此錯誤一般都是在jsp中使用了輸出流。

具體的原因就是
在jsp編譯成servlet之後在函數_jspService(HttpServletRequest request, HttpServletResponse response)的最後
有一段這樣的代碼
finally {
      if (_jspxFactory != null) _jspxFactory.releasePageContext(_jspx_page_context);
    }


這裏是在釋放在jsp中使用的對象,會調用response.getWriter(),因爲這個方法是和
response.getOutputStream()相沖突的!所以會出現以上這個異常。

解決的辦法,在使用完輸出流以後調用以下兩行代碼即可:

 

  1. out.clear();   
  2. out = pageContext.pushBody();  

即可 例如:

 

<%
  String did = request.getParameter("did");
  response.reset();
  response.setContentType("application/vnd.ms-excel");
  new ExportExcel().export(response.getOutputStream(), did);
  out.clear();//清空緩存的內容。
  out = pageContext.pushBody(); 
 %>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章