天天看點

從oracle中讀取blob圖檔資源

1.對應圖檔資源這張表建立對應的實體類

Ibatisblob.java

private byte[] picresult;
public byte[] getPicresult() {
        return picresult;
    }
public void setPicresult(byte[] picresult) {
        this.picresult = picresult;
    }
           

2.Dao層中的Dao.java和對應的映射檔案sqlmap.xml

Dao.java

public List pic(Map map) {
        return this.getSqlMapClientTemplate().queryForList("pic",map);
    }
           

sqlmap.xml

<sqlMap namespace="**" >
    <typeAlias alias="ibatisblob" type="cn.**.dao.IbatisBlob" />
<select id="pic" parameterClass="java.util.HashMap" resultClass="ibatisblob">
        select 
            pic
        from 
            PICRESULT t 
        where 
            t.ispic =  
        and t.id = #id# 
        and t.name= #name#
    </select>
           

3.service層處理

//擷取查詢
        lllDao llis = (Dao) SpringBeansUtil.getBean("lllDao");
        Map<String,String> map1 = new HashMap<String,String>();
        //填寫參數
        map1.put("id", id);
        map1.put("name", name);
        //1.接收二進制的圖檔
        List<IbatisBlob> imgResource = llis.pic(map1);
        String str1 = null;
        if(imgResource != null && imgResource.size()>) {
            byte[] picresult = imgResource.get().getPicresult();
            //加密運算之後 将byte[]轉化為base64
            byte[] encodeBase64 = Base64.encodeBase64(picresult);
            str1 = new String(encodeBase64);
        }
        //按照base64進行解密
        byte[] decodeBase64 = Base64.decodeBase64(encodeBase64);
        byte2image(decodeBase64, "G:\\1\\2.jpeg");
           

圖檔與位元組數組互相轉換

//圖檔到byte數組
  public byte[] image2byte(String path){
    byte[] data = null;
    FileImageInputStream input = null;
    try {
      input = new FileImageInputStream(new File(path));
      ByteArrayOutputStream output = new ByteArrayOutputStream();
      byte[] buf = new byte[];
      int numBytesRead = ;
      while ((numBytesRead = input.read(buf)) != -) {
      output.write(buf, , numBytesRead);
      }
      data = output.toByteArray();
      output.close();
      input.close();
    }
    catch (FileNotFoundException ex1) {
      ex1.printStackTrace();
    }
    catch (IOException ex1) {
      ex1.printStackTrace();
    }
    return data;
  }
///////////////////////////////////
//byte數組到圖檔
  public void byte2image(byte[] data,String path){
    if(data.length<||path.equals("")) return;
    try{
    FileImageOutputStream imageOutput = new FileImageOutputStream(new File(path));
    imageOutput.write(data, , data.length);
    imageOutput.close();
    System.out.println("Make Picture success,Please find image in " + path);
    } catch(Exception ex) {
      System.out.println("Exception: " + ex);
      ex.printStackTrace();
    }
  }
  //byte數組到16進制字元串
  public String byte2string(byte[] data){
    if(data==null||data.length<=) return "0x";
    if(data.length>) return "0x";
    StringBuffer sb = new StringBuffer();
    int buf[] = new int[data.length];
    //byte數組轉化成十進制
    for(int k=;k<data.length;k++){
      buf[k] = data[k]<?(data[k]+):(data[k]);
    }
    //十進制轉化成十六進制
    for(int k=;k<buf.length;k++){
      if(buf[k]<) sb.append("0"+Integer.toHexString(buf[k]));
      else sb.append(Integer.toHexString(buf[k]));
    }
    return "0x"+sb.toString().toUpperCase();
  }