环境:hibernate,myeclipse,sqlServer 2008R2等
前言:至于数据库的连接就不作代码说明了。
sessionFactory hibernate自动生成就可以了。
一、实体类
public class E_SOP_Detail {
private String SOP_VER_SID;
private String SOP_SID;
private int version;// SOP版本
private String default_Flag;// 默認是否有效
private String description;// SOP說明
private String enable_Flag;// 是否要生效
private Blob sop_doc; // SOP文件
private Date createDate;// 創建日期
private User createUser;// 創建人員
private Date updateDate;// 更新日期
private User updateUser;// 更新人員
}
二、配置文件
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="edison.esop.vo">
<class name="E_SOP_Detail" table="E_SOP_DETAIL">
<id name="SOP_VER_SID">
<generator class="native" />
</id>
<property name="SOP_SID" column="SOP_SID"></property>
<property name="version" column="version"></property>
<property name="default_Flag" column="default_Flag"></property>
<property name="description" column="description"></property>
<property name="sop_doc" type="java.sql.Blob">
<column name="SOP_DOC" />
</property>
<property name="createDate" column="createDate"></property>
<property name="createUser" column="createUser"></property>
<property name="updateDate" column="updateDate"></property>
<property name="updateUser" column="updateUser"></property>
</class>
</hibernate-mapping>
三、测试类
package junit.test;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Blob;
import java.sql.SQLException;
import java.util.Date;
import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.junit.Test;
import edison.esop.dao.HibernateSessionFactory;
import edison.esop.vo.E_SOP_Detail;
public class TestFile {
@Test
public void test() {//从数据库中下载二进制文件并存到指定位置
FileOutputStream fos = null;
InputStream is = null;
Session session = HibernateSessionFactory.getSession();
E_SOP_Detail eDetail = (E_SOP_Detail) session.get(E_SOP_Detail.class,
"1");
Blob blob = eDetail.getSop_doc();
try {
// 讀取blob內容寫入至檔中
is = blob.getBinaryStream();
fos = new FileOutputStream("WebRoot/files/aaa.pdf");
int b = -1;
while ((b = is.read()) != -1) {
fos.write(b);
}
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
is.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Test
public void test1() {//把本地文件上传到数据库指定的字段中
E_SOP_Detail eDetail = new E_SOP_Detail();
Session session = HibernateSessionFactory.getSession();
FileInputStream fis = null;
try {
fis=new FileInputStream("E:/spring.pdf");
byte[] buff = new byte[fis.available()];
// 将数据读到缓存中,这一步必不可少
fis.read(buff);
// 将字节数组转换为BLOB类型,这就可以在表的字段存数据了。
Blob blobContent = Hibernate.createBlob(buff);
eDetail.setSOP_VER_SID("1");
eDetail.setSOP_SID("1001");
eDetail.setSop_doc(blobContent);
eDetail.setCreateDate(new Date());
session.save(eDetail);// 保存
session.beginTransaction().commit();// 提交
} catch (HibernateException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {// 关闭流
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
四,正式service层和Action层代码
package edison.esop.service;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Blob;
import java.sql.SQLException;
import org.hibernate.Session;
import edison.esop.dao.BaseDaoImpl;
import edison.esop.dao.HibernateSessionFactory;
import edison.esop.vo.E_SOP_Detail;
import edison.esop.vo.Screen_Detail;
public class ScreenDetailServiceImpl extends BaseDaoImpl<Screen_Detail> implements ScreenDetailService{
/**
* 依照頁面傳過來的sopId,從數據庫中得到SOP實體
* @param sopId
*/
@SuppressWarnings("unchecked")
public E_SOP_Detail downloadSop(String sopId) {
InputStream is = null;
DataInputStream dis = null;
FileOutputStream fos = null;
DataOutputStream dos = null;
Session session = HibernateSessionFactory.getSession();
E_SOP_Detail eDetail = (E_SOP_Detail) session.get(E_SOP_Detail.class,
sopId);
Blob blob = eDetail.getSop_doc();
try {
// 讀取blob內容寫入至檔中
is = blob.getBinaryStream();
dis = new DataInputStream(is);
//以下這個項目所在的路徑,發布時,需對應修改.
// fos = new FileOutputStream("D:\\Users\\mis-zhimin-nb\\Workspaces\\apache-tomcat-7.0.70\\webapps\\esop\\"+eDetail.getDescription());
fos = new FileOutputStream("D:\\apache-tomcat-7.0.70\\webapps\\esop\\"+eDetail.getDescription());
dos = new DataOutputStream(fos);
int len;
while ((len = dis.read()) != -1) {
dos.write(len);
}
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
is.close();
dis.close();
fos.close();
dos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return eDetail;
}
}
*** action ***
package edison.esop.action;
import java.io.FileInputStream;
import java.io.InputStream;
import javax.servlet.ServletContext;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
import edison.esop.service.ScreenDetailService;
import edison.esop.service.ScreenDetailServiceImpl;
import edison.esop.vo.E_SOP_Detail;
public class DownloadAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
private ScreenDetailService sds = new ScreenDetailServiceImpl();
private String sopId;//等用戶傳一個SOP的名稱(Id)過來
private String contentType;
private long contentLength;
private String contentDisposition;
private InputStream inputStream;
public String execute() throws Exception {
E_SOP_Detail detailList = sds.downloadSop(sopId.trim());
if (sopId != null && !sopId.equals("")) {
//從數據庫中得到SOP實體
if (detailList != null) {
System.out.println(detailList.getSOP_SID()+" "+detailList.getDescription());
//將文件傳到jsp頁面並直接顯示
contentType="application/octet-stream";//直接使用應用程序打開
contentDisposition="filename="+detailList.getDescription();
ServletContext servletContext = ServletActionContext.getServletContext();
String fileName = servletContext.getRealPath(detailList.getDescription());
inputStream = new FileInputStream(fileName);
contentLength = inputStream.available();//返回的实际可读字节数,也就是总大小
return SUCCESS;
}
}
return INPUT;
}
public String getSopId() {
return sopId;
}
public void setSopId(String sopId) {
this.sopId = sopId;
}
public InputStream getInputStream() {
return inputStream;
}
public String getContentType() {
return contentType;
}
public long getContentLength() {
return contentLength;
}
public String getContentDisposition() {
return contentDisposition;
}
}
五,Action 配置文件说明
<package name="default" namespace="/" extends="struts-default">
<!-- 文件下載 -->
<action name="testDownload" class="edison.esop.action.DownloadAction">
<result type="stream">
<param name="inputName">inputStream</param>
<param name="bufferSize">204800</param>
</result>
<result name="input">/downFail.jsp</result>
</action>
</package>
六,页面
iframe src='testDownload.do' width='99%' height='2200px'
frameborder='0' scrolling='no' id='qSOP' ></iframe>