hibernate和oracle關於圖片的存儲和讀取

1:在applicationContext-*.xml的配置文件中

 <bean  id="nativeJdbcExtractor"  class="org.springframework.jdbc.support.nativejdbc.SimpleNativeJdbcExtractor" />
 <bean id="oracleLobHandler" class="org.springframework.jdbc.support.lob.OracleLobHandler" >
  <property name="nativeJdbcExtractor" ref="nativeJdbcExtractor"></property>
 </bean>

 
<bean id="lobHandler" lazy-init="true" class="org.springframework.jdbc.support.lob.DefaultLobHandler"/>

 

2:在sessionFactory Bean中

 <bean id="sessionFactory"
  class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  <property name="configLocation">
   <value>classpath:hibernate.cfg.xml</value>
  </property>
   <property name="lobHandler">
          <ref bean="oracleLobHandler" />
     </property>
 </bean>

加入這個屬性

  <property name="lobHandler">
          <ref bean="oracleLobHandler" />
     </property>
3:配置實體的hibernate的*.hbm.xml

 

   <property name="person18" type="org.springframework.orm.hibernate3.support.BlobByteArrayType" length="1048576000">
            <column name="PERSON18" />
        </property>

這個type是關鍵

 

在實體的 PERSON18 屬性 的類型是byte[] ,配置基本結束

4:怎樣存儲呢?

這裏是struts2+hibernate+spring 架構

 

在action中 private File[] upload; 這個upload其實就是jsp頁面的上傳<input type="file" name="upload"

在提交的時候 action中 開始把上傳的圖片set到實體的相關屬性中去

 InputStream in = null;

 

 in = new BufferedInputStream(new FileInputStream(this.getUpload()[0]), BUFFER_SIZE);
                  byte[] buffer = new byte[BUFFER_SIZE];
                  in.read(buffer);

//把上傳的圖片set到 實體中去
                  person.setPerson18(buffer);

最好直接save實體就可以了

 

5:讀取顯示在頁面

本人最初的想法是在action中把對象查出來,也就可以得到保存圖片的byte[] ,然後放到request中,在頁面直接讀取,結果失敗,

 

最後的解決辦法是

jsp頁面:

<img id="imgShow" width="100%" height="100%" src="imageAction.action?person01=${mk.person01}" alt="" /> 

 後臺action中

public String loadImage() throws Exception{
  InputStream fileInput = null;
  String person01 = request.getParameter("person01");
  File dir = new File(ServletActionContext.getServletContext().getRealPath("")+  "/1.jpg");
  Person p = super.getPersonService().getPersonById(Integer.parseInt(person01));
  byte[] bs = p.getPerson18();
  
  if(bs == null){
    fileInput = new BufferedInputStream(new FileInputStream(dir), 100*1024);
   byte[] buffer = new byte[100*1024];
   int i = fileInput.read(buffer);
   upLoadImg(buffer);
      fileInput.close();
  }else{
   upLoadImg(bs);
  }
        return null;

 

 

 

 

/**
  * 上傳圖片
  * @param bs
  */
public void upLoadImg(byte[] bs){
 
 ServletOutputStream out = null;
    InputStream in = null;
    InputStream in2 = null;
    try {
         //二進制輸出流
         response.setContentType("image/jpeg"); 
         
         //得到輸出流
         out = response.getOutputStream();
        in =  new java.io.ByteArrayInputStream(bs); 
         //強制刷新輸出流
        out.write(bs);
        out.flush();
    } catch ( IOException e ) {
         e.printStackTrace();
    } catch ( Exception e ) {
         e.printStackTrace();
    } finally {
         if ( in != null ) {
              try {
                   in.close();
              } catch ( IOException e ) {
                   e.printStackTrace();
              }
         }
         if ( out != null ) try {
              out.close();
         } catch ( IOException e ) {
              e.printStackTrace();
         }
    }
}

 

這樣就可以了

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章