存入數(shù)據(jù)庫(kù)中的圖片,如何讀出
2008年5月3日 Edited By DingDangXiaoMa
以sql server 為例:
在數(shù)據(jù)庫(kù)中存儲(chǔ)圖片使用的是image 字段, 我們?cè)谑褂胔ibernate 做映射時(shí),把這個(gè)字段映射為byte[]
把保存到數(shù)據(jù)庫(kù)中的圖片,輸出到頁(yè)面也就以流的形式展示出來(lái)。這里以servlet 為例:
這樣在.jsp或是html頁(yè)面中真接用<image src ="" /> 在src 部分寫(xiě)上servlet的鏈接地址就可以了。
以sql server 為例:
在數(shù)據(jù)庫(kù)中存儲(chǔ)圖片使用的是image 字段, 我們?cè)谑褂胔ibernate 做映射時(shí),把這個(gè)字段映射為byte[]
把保存到數(shù)據(jù)庫(kù)中的圖片,輸出到頁(yè)面也就以流的形式展示出來(lái)。這里以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();
}
上述的例子是運(yùn)行struts2 中session,里面存儲(chǔ)了byte[]的images .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();
}
這樣在.jsp或是html頁(yè)面中真接用<image src ="" /> 在src 部分寫(xiě)上servlet的鏈接地址就可以了。
posted on 2008-05-03 09:23 叮當(dāng)小馬 閱讀(270) 評(píng)論(0) 編輯 收藏 所屬分類: JSP/JAVA