今天上班做從數據庫查詢圖片到jsp頁面顯示,結果報java.lang.IllegalStateException: getOutputStream() has already been called 錯誤,折騰了將近半天的時間才弄出來。
在網上查閱了很多資料基本上都是一家之言,雖然說得都對,不過沒有源碼。所以我寫下來供大家查閱。廢話不多說,直接貼代碼:
1、jsp頁面(注:我用的是springMVC)
<img style="margin-top: 100px;" alt="投訴舉報流程圖" src="${stream }">
2、後臺java Action方法:
/**
* 投訴舉報流程圖
*
* @author wangmingyuan
* @return ModelAndView
* @serialData 2016年11月24日下午4:16:05
*/
@RequestMapping(value = "/complaintreportpicture")
public ModelAndView complaintreportpicture(String proId,HttpServletRequest request,HttpServletResponse response,Model model) {
Map<String, Object> vdata = new HashMap<String, Object>();
InputStream stream = wfComplaintReportService.findProcessPic("1");
try {
OutputStream output=response.getOutputStream();
int len=-1;
byte[]buf=new byte[1024];
while((len=stream.read(buf,0,1024))!=-1){
output.write(buf,0,len);
}
model.addAttribute("stream",stream);
output.flush();
return null;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return super.load("workflow/complaintreport/complaint_picture", vdata);
}
以上經過測試沒問題!!