1. 程式人生 > >Java從資料庫中讀取Blob物件圖片並顯示

Java從資料庫中讀取Blob物件圖片並顯示

import java.sql.*;
import java.io.*;
 
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.awt.image.AffineTransformOp;
import java.awt.geom.AffineTransform;
 
public class OracleQueryBean {
    private final String oracleDriverName = "oracle.jdbc.driver.OracleDriver";
 
    private Connection myConnection = null;
  
  
    private String strTabName;
  
    private String strIDName;
  
    private String strImgName;
  
    public OracleQueryBean(){
        try{
            Class.forName(oracleDriverName);
        }catch(ClassNotFoundException ex){
            System.out.println("載入jdbc驅動失敗,原因:" + ex.getMessage());
        }
    }
  
    public Connection getConnection(){
        try{
        //使用者名稱+密碼; 以下使用的Test就是Oracle裡的表空間
        //從配置檔案中讀取資料庫資訊
        GetPara oGetPara = new GetPara();
        String strIP = oGetPara.getPara("serverip");
        String strPort = oGetPara.getPara("port");
        String strDBName = oGetPara.getPara("dbname");
        String strUser = oGetPara.getPara("user");
        String strPassword = oGetPara.getPara("password");
      
        this.strTabName = oGetPara.getPara("tablename");
        this.strIDName = oGetPara.getPara("imgidname");
        this.strImgName = oGetPara.getPara("imgname");
      
        String oracleUrlToConnect ="jdbc:oracle:thin:@"+strIP+":"+strPort+":"+strDBName;
            this.myConnection = DriverManager.getConnection(oracleUrlToConnect, strUser, strPassword);
        }catch(Exception ex){
            System.out.println("Can not get connection:" + ex.getMessage());
            System.out.println("請檢測配置檔案中的資料庫資訊是否正確." );
        }
        return this.myConnection;
    }
  
    public byte[] GetImgByteById(String strID, int w, int h){
    //System.out.println("Get img data which id is " + nID);
    if(myConnection == null)
         this.getConnection();
    byte[] data = null;
    try {
            Statement stmt = myConnection.createStatement();
            ResultSet myResultSet = stmt.executeQuery("select " + this.strIDName + " from " + this.strTabName + " where " + this.strIDName + "=" + strID);
          
            StringBuffer myStringBuffer = new StringBuffer();
            if (myResultSet.next()) {
                java.sql.Blob blob = myResultSet.getBlob(this.strImgName);
                InputStream inStream = blob.getBinaryStream();
                try {
                    long nLen = blob.length();
                    int nSize = (int) nLen;
                    //System.out.println("img data size is :" + nSize);
                    data = new byte[nSize];
                    inStream.read(data);
                    inStream.close();
                } catch (IOException e) {
                    System.out.println("獲取圖片資料失敗,原因:" + e.getMessage());
                }
              
                data = ChangeImgSize(data, w, h);
            }
            System.out.println(myStringBuffer.toString());
            myConnection.commit();
            myConnection.close();
        } catch (SQLException ex) {
            System.out.println(ex.getMessage());
        }
        return data;
    }

    public byte[] GetImgByteById(String strID){
    //System.out.println("Get img data which id is " + nID);
    if(myConnection == null)
         this.getConnection();
    byte[] data = null;
    try {
            Statement stmt = myConnection.createStatement();
            ResultSet myResultSet = stmt.executeQuery("select " + this.strIDName + " from " + this.strTabName + " where " + this.strIDName + "=" + strID);
          
            StringBuffer myStringBuffer = new StringBuffer();
            if (myResultSet.next()) {
                java.sql.Blob blob = myResultSet.getBlob(this.strImgName);
                InputStream inStream = blob.getBinaryStream();
                try {
                    long nLen = blob.length();
                    int nSize = (int) nLen;
                    data = new byte[nSize];
                    inStream.read(data);
                    inStream.close();
                } catch (IOException e) {
                    System.out.println("獲取圖片資料失敗,原因:" + e.getMessage());
                }
            }
            System.out.println(myStringBuffer.toString());
            myConnection.commit();
            myConnection.close();
        } catch (SQLException ex) {
            System.out.println(ex.getMessage());
        }
        return data;
    }

    private byte[] ChangeImgSize(byte[] data, int nw, int nh){
    byte[] newdata = null;
    try{
         BufferedImage bis = ImageIO.read(new ByteArrayInputStream(data));
            int w = bis.getWidth();
            int h = bis.getHeight();
            double sx = (double) nw / w;
            double sy = (double) nh / h;
            AffineTransform transform = new AffineTransform();
            transform.setToScale(sx, sy);
            AffineTransformOp ato = new AffineTransformOp(transform, null);
            //原始顏色
            BufferedImage bid = new BufferedImage(nw, nh, BufferedImage.TYPE_3BYTE_BGR);
            ato.filter(bis, bid);         
            //轉換成byte位元組
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ImageIO.write(bid, "jpeg", baos);
            newdata = baos.toByteArray();
    }catch(IOException e){
         e.printStackTrace();
    }
    return newdata;
    }
}