写入Blob字段和写入其它类型字段的方式非常不同,因为Blob自身有一个cursor,你必须使用cursor对blob进行操作,因而你在写入Blob
1. bolb类型字段说明:
写入Blob字段和写入其它类型字段的方式非常不同,因为Blob自身有一个cursor,你必须使用cursor对blob进行操作,因而你在写入Blob之前,必须获得cursor才能进行写入,那么如何获得Blob的cursor呢?
这需要你先插入一个empty的blob,这将创建一个blob的cursor,然后你再把这个empty的blob的cursor用select查询出来,这样通过两步操作,你就获得了blob的cursor,,可以真正的写入blob数据了。
2. Bolb类型字段保存:
Hibernate的配置文件就不写了 ,需要将Blob字段了类型设为java.sql.Blob,下面直接上代码
悟空CRM是一种客户关系管理系统软件.它适应Windows、linux等多种操作系统,支持Apache、Nginx、IIs多种服务器软件。悟空CRM致力于为促进中小企业的发展做出更好更实用的软件,采用免费开源的方式,分享技术与经验。 悟空CRM 0.5.5 更新日志:2017-04-21 1.修复了几处安全隐患; 2.解决了任务.日程描述显示问题; 3.自定义字段添加时自动生成字段名
284
public void save(ZyglBlxx bean, InputStream ins) throws WebServiceException {
// TODO Auto-generated method stub
Session session = this.getHibernateTemplate().getSessionFactory().openSession();
//ins.
//out.write(b)
try {
bean.setDoccontent(BLOB.getEmptyBLOB());
Transaction tr = session.beginTransaction();
bean.setVId(WebServiceEditUtils.getPk());
session.save(bean);
session.flush();
session.refresh(bean, LockMode.UPGRADE);
if(ins!=null){
SerializableBlob sb = (SerializableBlob)bean.getDoccontent();
BLOB b = (BLOB)sb.getWrappedBlob();
OutputStream out = b.getBinaryOutputStream();
int len=-1;
byte[] bt = new byte[2048]; //可以根据实际情况调整,建议使用1024,即每次读1KB
while((len=(ins.read(bt))) != -1) {
out.write(bt,0,len); //建议不要直接用os.write(bt)
}
out.flush();
ins.close();
out.close();
}
session.flush();
tr.commit();
session.close();
} catch (IOException e) {
e.printStackTrace();
throw ExceptionManager.getExcption("18");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw ExceptionManager.getExcption("19");
}
}
为了保存文件时比较方便我这里的参数直接接收为InputStream,其他类型类似
Hibernate 的详细介绍:请点这里
Hibernate 的下载地址:请点这里
Hibernate 中文手册 PDF

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号