java中用Blob的資料類型向資料庫插入圖片

來源:互聯網
上載者:User

package com.vnet.flyed.text;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.SQLException;
import java.util.List;

import oracle.sql.BLOB;

import org.hibernate.Hibernate;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.lob.SerializableBlob;

import com.vnet.flyed.factory.HibernateSessionFactory;
import com.vnet.flyed.pojo.SLB/VPC Operations and Maintenance Systemuser;

public class HibernateTest {
 public static void main(String[] args) {
  
  Configuration config = new Configuration().configure();
  Session session = HibernateSessionFactory.getSession();
  SLB/VPC Operations and Maintenance Systemuser user = new SLB/VPC Operations and Maintenance Systemuser();
  Transaction tr = session.beginTransaction();
  user.setId(100);
  user.setName("flyed");
  user.setPassword("flyed");
  try {
   user.setImage(oracle.sql.BLOB.empty_lob());
  } catch (SQLException e1) {
   // TODO Auto-generated catch block
   e1.printStackTrace();
  }
  
  user.setResume(Hibernate.createClob(""));
  session.save(user);
  session.flush();
  try {
   session.refresh(user, LockMode.UPGRADE);
   SerializableBlob sb = (SerializableBlob) user.getImage();
   // 需要調用它的getWrappedBlob來轉型
   BLOB blob = (BLOB) sb.getWrappedBlob();
   // 將某個問件讀入後,寫到Blob欄位的輸出資料流中
   OutputStream out = blob.getBinaryOutputStream();
   FileInputStream imgis = new FileInputStream("d:/tom.gif");
   byte[] buf = new byte[102400]; // 10k緩衝
   int len;
   while ((len = imgis.read(buf)) > 0) {
    out.write(buf, 0, len);
   }
   imgis.close();
   out.close();
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  session.save(user);
  tr.commit();
  
  String hql = "from SLB/VPC Operations and Maintenance Systemuser where id=100";
  Query query = session.createQuery(hql);
  List list = query.list();
  if (list.size() > 0) {
   SLB/VPC Operations and Maintenance Systemuser user1 = (SLB/VPC Operations and Maintenance Systemuser) list.get(0);
   try {
    InputStream is = user1.getImage().getBinaryStream();
    OutputStream os = new FileOutputStream("d:/a.jpg");
    byte[] buf = new byte[1024];
    int i = 0;
    while ((i = is.read(buf)) != -1) {
     os.write(buf, 0, i);
    }

   } catch (SQLException e) {   
    e.printStackTrace();
   } catch (FileNotFoundException e) { 
    e.printStackTrace();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
  session.close();
 }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.