package com.xingao.action.workflow; import java.io.BufferedInputStream; import java.io.ByteArrayInputStream; import java.io.InputStream; import java.io.UnsupportedEncodingException; import java.sql.Blob; import java.sql.SQLException; import javax.annotation.Resource; import org.springframework.stereotype.Controller; import com.opensymphony.xwork2.ActionSupport; import com.xingao.dao.WWorkflowfileDao; import com.xingao.entity.W_workflowfile; import com.xingao.util.XingaoException; /** * 流程文档下载 * @author think * */@SuppressWarnings("serial") @Controllerpublic class WorkflowFileDownload extends ActionSupport{ @Resource WWorkflowfileDao wworkflowfiledao; //input private int workFid=0;//TODO 前台传入的当前流程的id private String lastWriteTime=null;//TODO 前台传入的文档最后写入时间 private String thisFileName=null; //output private String fileName;//TODO 初始的通过param指定的文件名属性 private Blob blob;//TODO 流程文档 private W_workflowfile w=new W_workflowfile(); private String msg="此文档不存在!"; 毕业论文 //TODO 获取输入流 public InputStream getInputStream(){ if(lastWriteTime!=null){ //通过lastWriteTime和workflow的id来得到W_workflowfile try { w=wworkflowfiledao.findWWorkflowfileById(workFid, lastWriteTime); } catch (XingaoException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(w.getId().getName()); System.out.println(w.getLastWriteTime()); if(null==w){ } }else if(thisFileName!=null){ //将文件名编码---防止乱码 String thisFileName1; try { thisFileName1 = java.net.URLDecoder.decode(thisFileName, "utf-8"); thisFileName=thisFileName1; } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } //通过文件名和流程的id来查询W_workflowfile try { w=wworkflowfiledao.findWWorkflowfileByWWFlowIdAndFileName(workFid, thisFileName); } catch (XingaoException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(w.getId().getName()); System.out.println(w.getLastWriteTime()); if(null==w){ } } blob=w.getFiles(); InputStream inputstream=new ByteArrayInputStream(new byte[4096]); try { inputstream=blob.getBinaryStream(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return inputstream; } public String execute() { System.out.println(workFid); System.out.println(lastWriteTime); System.out.println(thisFileName); if(getInputStream()==null){ return "error"; }else{ return "success"; } } public void setFileName(String fileName) { this.fileName = fileName; } /** 提供转换编码后的供下载用的文件名 */ //TODO 获取要下载的文件名称 public String getDownloadFileName() { String downFileName = w.getId().getName(); try { //TODO 防止乱码 downFileName = new String(downFileName.getBytes(), "ISO8859-1"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return downFileName; } public String getLastWriteTime() { return lastWriteTime; } public void setLastWriteTime(String lastWriteTime) { this.lastWriteTime = lastWriteTime; } public Blob getBlob() { return blob; } public void setBlob(Blob blob) { this.blob = blob; } public W_workflowfile getW() { return w; } public void setW(W_workflowfile w) { this.w = w; } public String getFileName() { return fileName; } public int getWorkFid() { return workFid; } public void setWorkFid(int workFid) { this.workFid = workFid; } public String getThisFileName() { return thisFileName; } public void setThisFileName(String thisFileName) { this.thisFileName = thisFileName; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } }
配置文件:
Java code?<!-- 流程文档的下载 --> <action name="download" class="com.xingao.action.workflow.WorkflowFileDownload"> <!-- 初始文件名 --> <param name="fileName">Struts中文附件.txt</param> <result name="success" type="stream"> <param name="contentType">text/plain</param> <param name="inputName">inputStream</param> <!-- 使用经过转码的文件名作为下载文件名,downloadFileName属性 对应action类中的方法 getDownloadFileName() --> <param name="contentDisposition">attachment;filename="${downloadFileName}"</param> <param name="bufferSize">4096</param> </result> </action>
<param name="contentType">text/plain</param>
<param name="inputName">inputStream</param>
改为
<param name="contentType">application/vnd.ms-excel</param>
<param name="inputName">excelStream</param>
blob=w.getFiles(); //这里你是在干嘛了?初始化了一个输入流 InputStream inputstream=new ByteArrayInputStream(new byte[4096]); try { //在这里将初始化的流对象覆盖了,初始化又有何意义? inputstream=blob.getBinaryStream(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return inputstream; }
你可以把读取到的blob字段流信息获取后赋值给一个中间变量,然后关闭连接,接着在进行操作。在客户那边,晚上时间是不会有人使用这个系统的,就造成了系统中原先没有考虑到的这个情况。
1、在数据库的url中加入autoReconnect=false;
在每次调用getSession()方法时判断session.isClosed()是否为真,
2、改用C3P0连接池,这个连接池会自动处理数据库连接被关闭的情况。要使用C3P0很简单,先从Hibernate里把c3p0-0.8.3.jar复制到项目的lib目录中,再在hibernate.properties里去掉hibernate.c3p0开头的那些属性的注释(使用缺省值或自己需要的数值),这样Hibernate就会自动使用C3P0代替内置的连接池了。到目前为止前面的问题没有再出现过。