这是我们项目用的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 {
……
}