(高分)java中实现把word保存到oracle到的blob字段,请高手给出代码!

2024-11-07 06:31:49
有1个网友回答
网友(1):

这是我们项目用的hibernate实现代码,可以把任何文件当作二进制流写进数据库。纯java的也应该类似的。
Attachment是一个对象
class Attachment{
private Long id;
private Longowner;
private Blob content;
}
实现代码:
InputStream in = null;
OutputStream out = null;
Attachment attach = new Attachment();
try {
in = attachment.getContent().getBinaryStream();
byte[] data = new byte[(int) attachment.getContent().length()];
in.read(data);

attach.setContent(Hibernate.createBlob(data));
attach.setOwner(attachment.getOwner());
getHibernateTemplate().save(attach);
getHibernateTemplate().flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
……
}