电竞比分网-中国电竞赛事及体育赛事平台

分享

Hibernate將文件二進制存入mysql

 LibraryPKU 2014-04-07
二進制大型對象(BLOB)列是MySQL的秘密武器之一。這些列中存儲了二進制的數據,你可以象其它普通的數據類型一樣來檢索和操縱它。根據MySQL指南有關資料,BLOB是一個二進制大型對象,它能容納不同大小的數據。事實上,MySQL有四種BLOB類型:

◆tinyblob:僅255個字符
◆blob:最大限制到65K字節(jié)
◆mediumblob:限制到16M字節(jié)
◆longblob:可達4GB


Java代碼
  1. String fname = "c:\\itanger.gif";//要入庫的文件    
  2. File f = new File(fname);    
  3. FileInputStream fin = new FileInputStream(f);    
  4. tad.setImage(Hibernate.createBlob(fin));   
  1. String fname = "c:\\itanger.gif";//要入庫的文件   
  2. File f = new File(fname);   
  3. FileInputStream fin = new FileInputStream(f);   
  4. tad.setImage(Hibernate.createBlob(fin));   


Java代碼
  1. public void doGet(HttpServletRequest request,HttpServletResponse response){   
  2.         User user=(User)session.load(User.classnew Integer(1));   
  3.         Blob photo=user.getPhoto();   
  4.         InputStream in=photo.getBinaryStream();   
  5.         OutputStream out=response.getOutputStream();   
  6.         byte [] buf=new byte[1024];   
  7.         int len;   
  8.         while((len=in.read(buf))!=-1){   
  9.             out.write(buf, 0, len);   
  10.         }   
  11.         in.close();   
  12.         out.close();   
  13.     }  

    本站是提供個人知識管理的網絡存儲空間,所有內容均由用戶發(fā)布,不代表本站觀點。請注意甄別內容中的聯系方式、誘導購買等信息,謹防詐騙。如發(fā)現有害或侵權內容,請點擊一鍵舉報。
    轉藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多