现在的Activiti流程中没有提供给我们下载流程文件(.bpmn)和流程图(.png)的方法,所以研究了一下,用传统的struts2下载结合jdbc的知识可以实现我们想要的效果。
1.首先,在action的头文件中加入(PreparedStatement 和ResultSet 都是java.sql的,而sessionFaction来自于hibernate):
private File file; //上传的文件
private String fileFileName; //文件名称
private SessionFactory sessionFactory;
private PreparedStatement pstmt = null;
private ResultSet rs = null;
2.然后在action中加入如下代码:
//下载流程定义图片
@SuppressWarnings("null")
public String download() {
FileOutputStream fos = null;
String realpath = getServletContext().getRealPath("/Uplaod");
//String savePath = UploadUtil.generateSavePath(realpath, fileName);
String filePath = realpath+File.separator+fileFileName;
try{
Session session = sessionFactory.getCurrentSession();
rs=session.doReturningWork(
new ReturningWork<ResultSet>() {
@Override
public ResultSet execute(Connection connection) throws SQLException {
String sql="select agb.BYTES_ from act_ge_bytearray agb where agb.NAME_=? and agb.DEPLOYMENT_ID_=?";
pstmt=connection.prepareStatement(sql);
pstmt.setString(1, fileFileName);
pstmt.setString(2, deployId);
rs=pstmt.executeQuery();
return rs;
}
}
);
while (rs.next()){
System.out.println("Read text document...");
BufferedReader br=new BufferedReader(rs.getCharacterStream("BYTES_"));
String str=null;
while((str=br.readLine())!=null){
System.out.println(str);
}
System.out.println("Read text document OK!");
System.out.println("Read image file...");
file = new File(filePath);
if(!file.exists())
{
file.createNewFile(); //如果文件不存在,则创建
}
BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(file));
byte[] buf=new byte[1024];
BufferedInputStream bis=new BufferedInputStream(rs.getBinaryStream("BYTES_"));
int count=-1;
while((count=bis.read(buf, 0, 1024))!=-1){
bos.write(buf, 0, count);
}
bos.flush();
System.out.println("Read image file OK!");
bos.close();
}
}catch(Exception e){
System.out.println("[OutPutFile error : ]" + e.getMessage());
}
return "download";
}
public InputStream getDownloadFile() throws UnsupportedEncodingException{
try {
String realpath = getServletContext().getRealPath("/Uplaod");
//String savePath = UploadUtil.generateSavePath(realpath, fileName);
String filePath = realpath+File.separator+fileFileName;
File file = new File(filePath);
FileInputStream in = new FileInputStream(file);
return in;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
3.最后在对应的struts文件中加上传统的下载配置:
<result type="stream" name="download">
<param name="contentType">application/octet-stream;charset=ISO8859-1</param>
<param name="inputName">downloadFile</param>
<param name="contentDisposition">attachment;filename=${fileFileName}</param>
<param name="bufferSize">4096</param>
</result>