今天做了個導出excel表的功能。大概代碼如下:
ouputStream = response.getOutputStream();
wb.write(ouputStream);
ouputStream.flush();
ouputStream.close();
1
2
3
4
發現報錯
java.lang.IllegalStateException: getOutputStream() has already been called for this response
1
報錯原因
getOutputStream方法用於返回Servlet引擎創建的字節輸出流對象,Servlet程序可以按字節形式輸出響應正文。
getWriter方法用於返回Servlet引擎創建的字符輸出流對象,Servlet程序可以按字符形式輸出響應正文。
getOutputStream和getWriter這兩個方法互相排斥,調用了其中的任何一個方法后,就不能再調用另一方法。
解決方案有兩種:
在jsp頁面里清除response。
out.clear();
out = pageContext.pushBody(http://www.my516.com);
1
2
在controller層對應的方法上追加@ResponseBody。
---------------------
