分享

Hibernate将文件二进制存入mysql

 LibraryPKU 2014-04-07
二进制大型对象(BLOB)列是MySQL的秘密武器之一。这些列中存储了二进制的数据,你可以象其它普通的数据类型一样来检索和操纵它。根据MySQL指南有关资料,BLOB是一个二进制大型对象,它能容纳不同大小的数据。事实上,MySQL有四种BLOB类型:

◆tinyblob:仅255个字符
◆blob:最大限制到65K字节
◆mediumblob:限制到16M字节
◆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.     }  

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多