下载aActiviti中的流程文件和流程图

本文详细介绍了如何通过传统Struts2下载Activiti流程定义的图片及流程文件,包括文件操作、数据库交互及下载配置,实现流程可视化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

现在的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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值