2008年5月3日 Edited By DingDangXiaoMa
以sql server 為例:
在數據庫中存儲圖片使用的是image 字段, 我們在使用hibernate 做映射時,把這個字段映射為byte[]
把保存到數據庫中的圖片,輸出到頁面也就以流的形式展示出來。這里以servlet 為例:
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Map session= ActionContext.getContext().getSession();
response.setContentType("image/jpeg");
OutputStream out = null;
out = response.getOutputStream();
out.write((byte [])session.get("images"));
out.flush();
out.close();
}
上述的例子是運行struts2 中session,里面存儲了byte[]的images .
這樣在.jsp或是html頁面中真接用<image src ="" /> 在src 部分寫上servlet的鏈接地址就可以了。