我有問題想獲取從數據庫中的圖像,然後顯示在JSP頁面中他們:如何從數據庫中查看jsp頁面中的多個圖像?
ImageAction:
public class ImageAction {
private byte[] itemImage;
public byte[] getItemImage() {
return itemImage;
}
public void setItemImage(byte[] itemImage) {
this.itemImage = itemImage;
}
public void execute() throws Exception{
try {
HttpServletResponse response = ServletActionContext.getResponse();
response.reset();
response.setContentType("multipart/form-data");
byte[] imgData =(byte[])ServletActionContext.getRequest().getSession()
.getAttribute("imageData");
System.out.println("imgData :: "+imgData);
itemImage = imgData;
ServletActionContext.getRequest().getSession().removeAttribute("imageData") ;
OutputStream out = response.getOutputStream();
out.write(itemImage);
out.flush();
out.close();
} catch (Exception e) {
System.out.println("error :: ");
e.printStackTrace();
}
// return "success";
}
}
的jsp:
<tr >
<td> <%= map.get(mapKey) %> </td>
<td colspan="1" >
<img src="<s:url value="ImageAction" />" width="115" border="0" />
</td>
</tr>
您的JSP頁面上需要一些機制將'byte []'數據轉換爲圖像 –
使用'stream'結果。 –
由於到目前爲止發佈的答案相當黑客和可怕,下面是簡單的JSP/Servlet方式的詳細解釋,完全獨立於Struts或您正在使用的任何MVC框架:http://stackoverflow.com/questions/2340406/如何對檢索和顯示圖像從 - 一個數據庫-IN-A-JSP頁面 – BalusC