struts hibernate 显示图片

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[length];//设定字节数组长度
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[i]);//输出到页面
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标签-->
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值