二進制大型對象(BLOB)列是MySQL的秘密武器之一。這些列中存儲了二進制的數(shù)據(jù),你可以象其它普通的數(shù)據(jù)類型一樣來檢索和操縱它。根據(jù)MySQL指南有關(guān)資料,BLOB是一個二進制大型對象,它能容納不同大小的數(shù)據(jù)。事實上,MySQL有四種BLOB類型:
◆tinyblob:僅255個字符
◆blob:最大限制到65K字節(jié)
◆mediumblob:限制到16M字節(jié)
◆longblob:可達4GB
- String fname = "c:\\itanger.gif";
- File f = new File(fname);
- FileInputStream fin = new FileInputStream(f);
- tad.setImage(Hibernate.createBlob(fin));
- String fname = "c:\\itanger.gif";
- File f = new File(fname);
- FileInputStream fin = new FileInputStream(f);
- tad.setImage(Hibernate.createBlob(fin));
- public void doGet(HttpServletRequest request,HttpServletResponse response){
- User user=(User)session.load(User.class, new Integer(1));
- Blob photo=user.getPhoto();
- InputStream in=photo.getBinaryStream();
- OutputStream out=response.getOutputStream();
- byte [] buf=new byte[1024];
- int len;
- while((len=in.read(buf))!=-1){
- out.write(buf, 0, len);
- }
- in.close();
- out.close();
- }
|