---
title: "struts hibernate 显示图片"
author: "premier_gu"
date: "2011-04-24T18:01:12Z"
tags: []
categories: ["编程语言"]
slug: "struts-hibernate"
draft: false
lastmod: "2011-04-24T18:16:07Z"
id: 1014341
url: "https://premier-gu.iteye.com/blog/1014341"
---

Action：
1./**  
2.* test show image in jsp  
7.*/  
8.public ActionForward showImage(ActionMapping mapping, ActionForm form,   
9.    HttpServletRequest request, HttpServletResponse response) {   
10.   int length = 0;   
11.   ServletOutputStream toClient = null;   
12.   byte buf=null;   
13.   CommodityService service = new CommodityService();   
14.   List list = service.getCommodityByX(null, null);   
15.   Commodity commodity = (Commodity) list.get(2);//取得商品对象   
16.   Blob image = commodity.getImage();//取得以blob格式存储图片信息   
17.   try {   
18.    //is = image.getBinaryStream();//取得二进制流   
19.    length = (int)image.length();//取得流中的可用字节总数   
20.    //buf=new byte;//设定字节数组长度   
21.    buf=image.getBytes(1,length);//获取Blob字节数组   
22.    response.setContentType("image/jpeg");   
23.    toClient=response.getOutputStream();//获取输出流   
24.    for (int i = 0; i < buf.length; i++) {   
25.     toClient.write(buf*);//输出到页面   
26.    }   
27.    toClient.close();//关闭输出流   
28.   } catch (SQLException se) {   
29.    se.printStackTrace();   
30.   } catch (IOException e) {   
31.    e.printStackTrace();   
32.   }   
33.   return mapping.findForward("showImage");//跳转到指定页面   
34.}  

===========================================================

配置文件：
1.<?xml version="1.0" encoding="UTF-8"?>  
2.<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"    
3.  
4."http://struts.apache.org/dtds/struts-config_1_2.dtd">  
5.  
6.<struts-config>  
7.<data-sources />  
8.<form-beans>  
9.   <form-bean name="userActionForm" type="com.mt.struts.UserActionForm" />  
10.   <form-bean name="commodityForm" type="com.mt.struts.CommodityForm" />  
11.</form-beans>  
12.  
13.<global-exceptions />  
14.<global-forwards>  
15.   <!-- <forward name="error" path="/error.jsp" />-->  
16.</global-forwards>  
17.<action-mappings>  
18.   <action attribute="userActionForm" input="/form/userAction.jsp" name="userActionForm" parameter="postMethod"    
19.  
20.path="/userAction" scope="request" type="com.mt.struts.UserAction">  
21.    <forward name="ok" path="/index.jsp" />  
22.    <forward name="login" path="/login.jsp" />  
23.    <forward name="userList" path="/back/userlist.jsp"></forward>  
24.   </action>  
25.   <action attribute="commodityForm" input="/form/commodity.jsp" name="commodityForm" parameter="postMethod"    
26.  
27.path="/commodityAction" scope="request" type="com.mt.struts.CommodityAction">  
28.    <forward name="goodList" path="/goodlist.jsp" />  
29.    <forward name="goodAdd" path="/back/goodadd.jsp" />  
30.    <forward name="showImage" path="/back/showimage.jsp" />  
31.   </action>  
32.</action-mappings>  
33.  
34.<message-resources parameter="com.mt.struts.ApplicationResources" />  
35.</struts-config> 

===========================================================
JSP: 
1.<body>  
2.    This a struts page. <br>  
3.    <html:image src="/commodityAction.do?postMethod=showImage"></html:image>  
4.</body><!--这里用的是struts标签-->