1.字段类型 private java.sql.Blob pactByte;
2.配置文件类型 <property name="pactByte" type="java.sql.Blob"> <column name="PACT_byte" /> </property>
3.action层 service层与平常操作无异
4.dao层上传
public void save(Pact pact,java.io.File file) throws Exception {
try {
Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
session.beginTransaction();
pact.setPactByte(Hibernate.createBlob(new byte[1]));
session.save(pact);
session.flush();
session.refresh(pact, LockMode.UPGRADE);
//将java.sql.blob 转化为oracle.sql.blob
SerializableBlob sc = (SerializableBlob) pact.getPactByte();
java.sql.Blob wrapclob = sc.getWrappedBlob();
oracle.sql.BLOB clob = (oracle.sql.BLOB) wrapclob;
//将oracle.sql.blob类型重新放回字段中
OutputStream fout = clob.getBinaryOutputStream();
java.io.InputStream fin =new java.io.FileInputStream(file);
byte[] buff = new byte[fin.available()];
fin.read(buff);
fin.close();
fout.write(buff);
fout.close();
session.save(pact);
} catch (Exception re) {
throw re;
}
}
5.action层下载方法
//下载合同文件
public InputStream getDownloadFile() throws Exception{
Pact pact = this.pactBiz.findById(pact_id);
Blob blob = pact.getPactByte();
InputStream fin=blob.getBinaryStream();
ServletActionContext.getResponse().setHeader("Content-Disposition", "attachment;filename=" + java.net.URLEncoder.encode(pact.getPactName(), "UTF-8"));
ServletActionContext.getResponse().setContentType("application/msword");
return fin;
}
public String downloadpact() {
return "success";
}