ITeye博客

struts hibernate 显示图片

premier_gu · 2011-04-24 18:01 · 编程语言 · Markdown

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.
2.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18. <action attribute="userActionForm" input="/form/userAction.jsp" name="userActionForm" parameter="postMethod"
19.
20.path="/userAction" scope="request" type="com.mt.struts.UserAction">
21.
22.
23.
24.
25. <action attribute="commodityForm" input="/form/commodity.jsp" name="commodityForm" parameter="postMethod"
26.
27.path="/commodityAction" scope="request" type="com.mt.struts.CommodityAction">
28.
29.
30.
31.
32.
33.
34.
35.

=========================================================== JSP: 1.
2. This a struts page.
3. <html:image src="/commodityAction.do?postMethod=showImage"></html:image>
4.

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。