nicky

积水成海,滴水穿石。

导航

<2014年4月>
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910

统计

公告

信心十足

常用链接

留言簿(3)

随笔档案

文章档案

搜索

最新评论

阅读排行榜

评论排行榜

struts2+hibernate实现图片的上传和显示

struts2+hibernate实现图片的上传和显示

       这里的上传是指将图片上传到数据库,显示是把多张数据库的图片显示在一个jsp文件里。

    图片在数据库里面用blob类型表示,在mysql里面blob能够存储的大小

 类型  大小(单位:字节)
 TinyBlob  最大 255
 Blob  最大 65K
 MediumBlob  最大 16M
 LongBlob  最大 4G

    数据是网上找的,不保证一定对,做参考吧。

    在hibernate中blob被映射成byte[],下面是例子
  1. public class Book  implements java.io.Serializable {
  2.      private String id;
  3.      private BookChildKind bookChildKind;
  4.      private BookKind bookKind;
  5.      private String bookName;
  6.      private int price;
  7.      private String bookAuther;
  8.      private String bookPublisher;
  9.      private byte[] bookImg;    //这个对应数据库blob类型的字段
  10.      private Date buyTime;
  11.      private int totalCount;
  12.      private String bookDescribe;
  13.      private int sellCount;
  14. }

    上传图片到本地硬盘的过称我之前的文章写过了http://blog.csdn.net/zhiweiv/archive/2008/10/16/3085834.aspx,这里就不写了。主要的是把数据存入到数据库。
  1.         byte buffer[]=new byte[(int)bookImg.length()];
  2.         FileInputStream in=new FileInputStream(bookImg);
  3.         in.read(buffer);
  4.         book.setBookImg(buffer);
     bookImg为图片上传到本地对应的File实例。

     然后是将数据库里面的图片读出来作为img的src显示出来,原理和以前那个struts2的图形验证码实现相同http://blog.csdn.net/zhiweiv/archive/2008/10/08/3035811.aspx。使用struts2的stream返回img的字节信息作为图片的src,这里的问题是一个页面有很多图片,有一个action提供返回指定id的数据库记录的图片字节流
  1.     public String getImg(){
  2.         Book book=bookDao.get(id);
  3.         inputStream=new ByteArrayInputStream(book.getBookImg());
  4.         return "img";
  5.     }
    但是struts2没有为img提供包装的标签,怎么动态的设置img的src呢??  原来还可以这样用~~~
  1. <img src="mainPageAction!getImg.action?id=<s:property value="id"/>"/>

    以前我一直不知道原来struts2的标签还可以这样用的

posted on 2009-04-23 15:48 nicky 阅读(6142) 评论(2)  编辑  收藏

评论

# re: struts2+hibernate实现图片的上传和显示 2014-04-11 22:50

搞了老半天,看了楼主的,这段顿时开窍!谢谢!  回复  更多评论   

# re: struts2+hibernate实现图片的上传和显示 2014-05-13 19:37 ss

ff  回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航: