<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 22, comments - 8, trackbacks - 0, articles - 0
       :: 首頁 ::  :: 聯(lián)系 :: 聚合  :: 管理

    hibernate存取圖片示例

    Posted on 2006-12-28 19:26 路易 閱讀(214) 評論(0)  編輯  收藏 所屬分類: Hibernate
    一般網(wǎng)站在處理用戶上傳圖片時通常采用兩種策略:一是直接把圖片存入數(shù)據(jù)庫中的Blob字段;二是數(shù)據(jù)庫中只存儲圖片的在服務(wù)器上的路徑信息?,圖片存放在分門別類的文件中,使用的時候從數(shù)據(jù)庫讀取路徑信息到頁面img元素即可.在此不討論兩種方案的優(yōu)劣,我只是寫了個hibernate的例子來實現(xiàn)第一種策略.例子很簡單,t_user表主要兩個字段,name和photo,其中photo字段類型為Blob.在此例中數(shù)據(jù)庫我采用mysql,oracle的Blob字段比較特殊,你必須自定義類型,具體的請自行搜索,這方面的資料很多.

    //User.java  

    package?com.denny_blue.hibernate;

    import?java.io.Serializable;
    import?java.sql.Blob;

    public?class?User?implements?Serializable{
    ?private?Integer?id;
    ?private?String?name;
    ?private?Blob?photo;
    ?/**
    ??*?@return?the?id
    ??*/
    ?public?User(){
    ?}
    ?public?Integer?getId()?{
    ??return?id;
    ?}
    ?/**
    ??*?@param?id?the?id?to?set
    ??*/
    ?public?void?setId(Integer?id)?{
    ??this.id?=?id;
    ?}
    ?/**
    ??*?@return?the?name
    ??*/
    ?public?String?getName()?{
    ??return?name;
    ?}
    ?/**
    ??*?@param?name?the?name?to?set
    ??*/
    ?public?void?setName(String?name)?{
    ??this.name?=?name;
    ?}
    ?/**
    ??*?@return?the?photo
    ??*/
    ?public?Blob?getPhoto()?{
    ??return?photo;
    ?}
    ?/**
    ??*?@param?photo?the?photo?to?set
    ??*/
    ?public?void?setPhoto(Blob?photo)?{
    ??this.photo?=?photo;
    ?}
    ?
    }


    類User有3個屬性,id,name,photo,相應(yīng)的getter和setter方法以及一個無參構(gòu)造函數(shù).應(yīng)該注意的是photo的類型java.sql.Blob

    相應(yīng)的user.hbm.xml應(yīng)該如下:

    <?xml?version="1.0"?>
    <!DOCTYPE?hibernate-mapping?PUBLIC
    ?"-//Hibernate/Hibernate?Mapping?DTD?3.0//EN"
    ?"
    http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd ">
    <hibernate-mapping
    ?package="com.denny_blue.hibernate">

    ?<class?name="com.denny_blue.hibernate.User"
    ????????table="t_user"
    ????????dynamic-update="true"
    ????????dynamic-insert="true"
    ????????batch-size="3">
    ??<id?name="id"
    ??????column="id"
    ??????type="java.lang.Integer">
    ???<generator?class="native"/>
    ??</id>
    ??<property?name="name"?column="name"?type="java.lang.String"?lazy="true"/>
    ??<property?name="photo"?column="photo"?type="java.sql.Blob"/>

    ?</class>

    </hibernate-mapping>

    對應(yīng)的hibernate.cfg.xml配置文件,不再列出,請參照hibernate文檔自行設(shè)定.

    OK,做了這一步,我們寫個測試類來進行單元測試:

    package?com.denny_blue.test;

    import?java.io.FileInputStream;
    import?java.io.FileNotFoundException;
    import?java.io.FileOutputStream;
    import?java.io.IOException;
    import?java.io.InputStream;
    import?java.sql.Blob;

    import?org.hibernate.Hibernate;
    import?org.hibernate.HibernateException;
    import?org.hibernate.Session;
    import?org.hibernate.SessionFactory;
    import?org.hibernate.Transaction;
    import?org.hibernate.cfg.Configuration;

    import?com.denny_blue.hibernate.User;

    import?junit.framework.TestCase;

    public?class?HibernateTest?extends?TestCase?{
    ????????private?Session?session;
    ?protected?void?setUp()?throws?Exception?{
    ??try{
    ???Configuration?config=new?Configuration().configure();
    ???SessionFactory?sf=config.buildSessionFactory();
    ???session=sf.openSession();
    ??}catch(HibernateException?e){
    ???e.printStackTrace();
    ??}
    ?}

    ?protected?void?tearDown()?throws?Exception?{
    ??try{
    ???session.close();
    ??}catch(HibernateException?e){
    ???e.printStackTrace();
    ??}
    ?}
    ?
    ?public?void?testSave()throws?FileNotFoundException,IOException{
    ??User?user=new?User();
    ??user.setName("jordan");
    ??FileInputStream?in=new?FileInputStream("C:\\test.gif");
    ??Blob?photo=Hibernate.createBlob(in);
    ??user.setPhoto(photo);
    ??Transaction?tx=null;
    ??try{
    ??tx=session.beginTransaction();
    ??session.saveOrUpdate(user);
    ??tx.commit();
    ??}catch(HibernateException?e){
    ???if(tx!=null)
    ????tx.rollback();
    ???e.printStackTrace();
    ??}finally{
    ???in.close();
    ??}
    ?}
    ?public?void?testLoad()throws?Exception{
    ??try{
    ???User?user=(User)session.load(User.class,?new?Integer(1));
    ???Blob?photo=user.getPhoto();
    ???InputStream?in=photo.getBinaryStream();
    ???FileOutputStream?out=new?FileOutputStream("C:\\out\\test2.gif");
    ???byte?[]?buf=new?byte[1024];
    ???int?len;
    ???while((len=in.read(buf))!=-1){
    ????out.write(buf,?0,?len);
    ???}
    ???in.close();
    ???out.close();
    ??}catch(HibernateException?e){
    ???e.printStackTrace();
    ??}
    ?}

    }
    我們讀取C盤目錄下的test.gif并存儲到數(shù)據(jù)庫中,然后再取出來寫入C:\out目錄,此時你可以查看下數(shù)據(jù)表中photo顯示為blob,表示已經(jīng)成功存入.值的注意的代碼片段就是:

    FileInputStream?in=new?FileInputStream("C:\\test.gif");
    ??Blob?photo=Hibernate.createBlob(in);
    我們這里是從磁盤中讀取圖片,實際應(yīng)用中你可以利用上傳組件得到圖片的2進制數(shù)據(jù)流,并利用Hibernate.createBlob方法來構(gòu)造相應(yīng)的Blob對象.而取圖片則使用

    InputStream?in=photo.getBinaryStream();

    這只是個簡單的測試類,如果我想從數(shù)據(jù)庫中取出圖片并現(xiàn)實在頁面上該如何做呢?其實也很簡單,我們先要寫一個servlet,在它的service方法中取出圖片,并"畫"到指定頁面上.

    package?com.easyjf.asp.action;

    import?java.io.InputStream;
    import?java.io.OutputStream;
    import?java.sql.Blob;

    import?javax.servlet.ServletException;
    import?javax.servlet.http.HttpServlet;
    import?javax.servlet.http.HttpServletRequest;
    import?javax.servlet.http.HttpServletResponse;

    import?org.hibernate.HibernateException;
    import?org.hibernate.Session;
    import?org.hibernate.SessionFactory;
    import?org.hibernate.cfg.Configuration;
    import?com.denny)blue.hibernate.User;


    public?class?Test?extends?HttpServlet?{

    ?/**
    ??*?Destruction?of?the?servlet.?<br>
    ??*/
    ?private?Session?session;
    ?public?void?destroy()?{
    ??try{
    ???session.close();
    ??}catch(HibernateException?e){
    ???e.printStackTrace();
    ??}
    ?}

    ?/**
    ??*?Initialization?of?the?servlet.?<br>
    ??*
    ??*?@throws?ServletException?if?an?error?occure
    ??*/
    ?public?void?init()?throws?ServletException?{
    ??try{
    ???Configuration?config=new?Configuration().configure();
    ???SessionFactory?sf=config.buildSessionFactory();
    ???session=sf.openSession();
    ??}catch(HibernateException?e){
    ???e.printStackTrace();
    ??}
    ?}
    ????public?void?doGet(HttpServletRequest?request,HttpServletResponse?response)
    ????{
    ?????try{
    ???User?user=(User)session.load(User.class,?new?Integer(1));
    ???Blob?photo=user.getPhoto();
    ???InputStream?in=photo.getBinaryStream();
    ???OutputStream?out=response.getOutputStream();
    ???byte?[]?buf=new?byte[1024];
    ???int?len;
    ???while((len=in.read(buf))!=-1){
    ????out.write(buf,?0,?len);
    ???}
    ???in.close();
    ???out.close();
    ??}catch(Exception?e){
    ???e.printStackTrace();
    ??}
    ????}

    }

    通過response.getOutputStream取得輸出流,其他就與上段代碼一致.servlet寫好了,怎么在頁面調(diào)用呢?那就更簡單啦,直接在頁面的img標簽的src屬性上調(diào)用該servlet即可,如:

    <img?id="test"?src="/servlet/Test"/>

    只有注冊用戶登錄后才能發(fā)表評論。


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 国产亚洲美日韩AV中文字幕无码成人 | 日本高清免费不卡视频| 亚洲色图校园春色| 久久午夜免费鲁丝片| 亚洲专区先锋影音| 久久免费看黄a级毛片| 亚洲成a人片在线观看播放| 中文字幕亚洲免费无线观看日本| 亚洲AV无码第一区二区三区| 免费国产在线视频| 亚洲色欲www综合网| 114一级毛片免费| 中文无码亚洲精品字幕| 午夜免费福利影院| 亚洲AV无码一区二区三区网址| 午夜一级毛片免费视频| 美女黄频a美女大全免费皮| 国产亚洲精品不卡在线| 日本在线免费观看| 亚洲乱码中文字幕小综合| 女人18毛片水最多免费观看| 免费国产黄网站在线观看动图| 中国亚洲女人69内射少妇| 亚洲成年人免费网站| 亚洲kkk4444在线观看| 亚洲AV无码不卡在线观看下载| 在线免费播放一级毛片| 亚洲精品国产肉丝袜久久| 四虎影视大全免费入口| www一区二区www免费| 日韩精品一区二区亚洲AV观看| 无码少妇一区二区浪潮免费| 国产精品亚洲专区一区| 久久夜色精品国产亚洲| 亚洲第一成年免费网站| 无码的免费不卡毛片视频 | 日韩免费电影在线观看| 国产精品美女免费视频观看| 亚洲黄色在线视频| 国产一区二区免费在线| 在线观看免费无码专区|