hibernate+mysql图片读取

时间:2022-09-19 23:40:01

在网上找了一些资料,自己又试验了一下,成功了!

大概过程就是,把图片文件用inputstream读取出来, 变成字节码文件,把字节码存储进数据库,在数据库里用CLOB数据类型存储图片。下面是代码:

 

hibernate+mysql图片读取package  image;
hibernate+mysql图片读取
hibernate+mysql图片读取
import  java.awt.image.BufferedImage;
hibernate+mysql图片读取
import  java.io.FileInputStream;
hibernate+mysql图片读取
import  java.io.FileNotFoundException;
hibernate+mysql图片读取
import  java.io.FileOutputStream;
hibernate+mysql图片读取
import  java.io.InputStream;
hibernate+mysql图片读取
import  java.sql.SQLException;
hibernate+mysql图片读取
import  java.util.Date;
hibernate+mysql图片读取
import  java.util.List;
hibernate+mysql图片读取
hibernate+mysql图片读取
import  javax.imageio.ImageIO;
hibernate+mysql图片读取
hibernate+mysql图片读取
import  many2many.HbnUtil;
hibernate+mysql图片读取
import  many2many.Student;
hibernate+mysql图片读取
hibernate+mysql图片读取
import  org.hibernate.Hibernate;
hibernate+mysql图片读取
import  org.hibernate.HibernateException;
hibernate+mysql图片读取
import  org.hibernate.Query;
hibernate+mysql图片读取
import  org.hibernate.Session;
hibernate+mysql图片读取
import  org.hibernate.Transaction;
hibernate+mysql图片读取
hibernate+mysql图片读取hibernate+mysql图片读取
public   class  Test  ... {
hibernate+mysql图片读取
hibernate+mysql图片读取hibernate+mysql图片读取    
/** *//**
hibernate+mysql图片读取     * 
@param args
hibernate+mysql图片读取     * 
@throws Exception 
hibernate+mysql图片读取     
*/

hibernate+mysql图片读取hibernate+mysql图片读取    
public static void main(String[] args) throws Exception ...{
hibernate+mysql图片读取        
//insert();
hibernate+mysql图片读取
        show();
hibernate+mysql图片读取    }

hibernate+mysql图片读取hibernate+mysql图片读取    
public static Person show() throws Exception...{
hibernate+mysql图片读取        Session s
=null;
hibernate+mysql图片读取        Transaction tran
=null;
hibernate+mysql图片读取        String hql
="from Person";
hibernate+mysql图片读取hibernate+mysql图片读取        
try...{
hibernate+mysql图片读取            s
=HbnUtil.getSession();        
hibernate+mysql图片读取            tran
=s.beginTransaction();
hibernate+mysql图片读取            Query query
=s.createQuery(hql);
hibernate+mysql图片读取            List list
=query.list();
hibernate+mysql图片读取            System.out.println(list.size());
hibernate+mysql图片读取            
int i=0;
hibernate+mysql图片读取hibernate+mysql图片读取            
for(Person person:(List<Person>) list)...{
hibernate+mysql图片读取                    
int len=(int) person.getBl().length();                    
hibernate+mysql图片读取                    
byte[] temp=person.getBl().getBytes(1, len);
hibernate+mysql图片读取                    String dir
="c:/temp/temp_"+person.getName()+".jpg";
hibernate+mysql图片读取                    FileOutputStream fos
=new FileOutputStream(dir);
hibernate+mysql图片读取                    fos.write(temp);
hibernate+mysql图片读取            }

hibernate+mysql图片读取            tran.commit();
hibernate+mysql图片读取hibernate+mysql图片读取        }
 catch (HibernateException e) ...{
hibernate+mysql图片读取            e.printStackTrace();
hibernate+mysql图片读取            tran.rollback();
hibernate+mysql图片读取hibernate+mysql图片读取        }
finally...{
hibernate+mysql图片读取            HbnUtil.releaseSession(s);
hibernate+mysql图片读取        }

hibernate+mysql图片读取        
return null;        
hibernate+mysql图片读取    }

hibernate+mysql图片读取hibernate+mysql图片读取    
public static void insert() throws Exception...{
hibernate+mysql图片读取        Session s
=null;
hibernate+mysql图片读取        Transaction tran
=null;
hibernate+mysql图片读取        
hibernate+mysql图片读取hibernate+mysql图片读取        
try...{
hibernate+mysql图片读取            FileInputStream fis
=new FileInputStream("c:/temp/112.jpg");
hibernate+mysql图片读取            System.out.println(fis.available());            
hibernate+mysql图片读取            Person person
=new Person();
hibernate+mysql图片读取            person.setName(
"teng");
hibernate+mysql图片读取            person.setBl(Hibernate.createBlob(fis));
hibernate+mysql图片读取            
hibernate+mysql图片读取            s
=HbnUtil.getSession();        
hibernate+mysql图片读取            tran
=s.beginTransaction();
hibernate+mysql图片读取            s.save(person);    
hibernate+mysql图片读取            fis.close();        
hibernate+mysql图片读取            tran.commit();
hibernate+mysql图片读取hibernate+mysql图片读取        }
 catch (HibernateException e) ...{
hibernate+mysql图片读取            e.printStackTrace();
hibernate+mysql图片读取            tran.rollback();
hibernate+mysql图片读取hibernate+mysql图片读取        }
finally...{
hibernate+mysql图片读取            HbnUtil.releaseSession(s);
hibernate+mysql图片读取        }
            
hibernate+mysql图片读取    }

hibernate+mysql图片读取
hibernate+mysql图片读取}

hibernate+mysql图片读取