Blob 二进制大对象 Clob字符串大对象
public void save(Login login,String loginPath,String description) throws IOException{
InputStream in=this.getClass().getResourceAsStream(loginPath);
byte[] buff=new byte[in.available()];
in.read(buff);
in.close();
//字节数组转换为Blob对象
login.setImage(Hibernate.createBlob(buff));
//转换为clob对象
login.setDescription(Hibernate.createClob(description));
session=HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
session.save(login);
session.getTransaction().commit();
}
public void get(int id,String targetPath) throws SQLException, IOException{
session=HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Login login=(Login) session.get(Login.class, id);
session.getTransaction().commit();
//读取Clob内容
Clob description=login.getDescription();
Reader r=description.getCharacterStream();
BufferedReader br=new BufferedReader(r);
String desc=null;
do {
desc=br.readLine();
if(desc==null)
break;
System.out.println(desc);
} while (desc!=null);
br.close();
r.close();
//Blob getBinaryStream()
InputStream in=login.getImage().getBinaryStream();
FileOutputStream out=new FileOutputStream(targetPath);
byte[] buff=new byte[1024];
while(in.read(buff)!=-1){
out.write(buff);
}
out.close();
in.close();
}