本文实例展示了Activiti流程图查看的实现方法,具体步骤如下所示:
1、测试用例查看图片代码如下:
public void viewImage() throws Exception {
// 创建仓库服务对对象
RepositoryService repositoryService = processEngine.getRepositoryService();
// 从仓库中找需要展示的文件
String deploymentId = "701";
List<String> names = repositoryService.getDeploymentResourceNames(deploymentId);
String imageName = null;
for (String name : names) {
if(name.indexOf(".png")>=0){
imageName = name;
}
}
if(imageName!=null){
// System.out.println(imageName);
File f = new File("e:/"+ imageName);
// 通过部署ID和文件名称得到文件的输入流
InputStream in = repositoryService.getResourceAsStream(deploymentId, imageName);
FileUtils.copyInputStreamToFile(in, f);
}
说明:
1) deploymentId为流程部署ID
2) resourceName为act_ge_bytearray表中NAME_列的值
3) 使用repositoryService的getDeploymentResourceNames方法可以获取指定部署下得所有文件的名称
4) 使用repositoryService的getResourceAsStream方法传入部署ID和文件名称可以获取部署下指定名称文件的输入流
5) 最后的有关IO流的操作,使用FileUtils工具的copyInputStreamToFile方法完成流程流程到文件的拷贝
2、web项目中在流程定义页面查看图片:
public String viewImage(){
InputStream in = repositoryService.getResourceAsStream.getImageStream(deploymentId,imageName);//此处方法实际项目应该放在service里面
HttpServletResponse resp = ServletActionContext.getResponse();
try {
OutputStream out = resp.getOutputStream();
// 把图片的输入流程写入resp的输出流中
byte[] b = new byte[1024];
for (int len = -1; (len= in.read(b))!=-1; ) {
out.write(b, 0, len);
}
// 关闭流
out.close();
in.close();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
说明:
1) deploymentId为流程部署ID,imageName为图片名称
2) 因为是从流程定义列表页面查看图片,id和imageName可以从流程定义(ProcessDefinition)中获取(String getDeploymentId();和 String getDiagramResourceName();)
3)web页面标签<a target="_blank" href="viewImage?deploymentId=1&imageName=imageName.png" rel="external nofollow" >查看流程图</a>
3、web项目查看当前流程图
public String viewCurrentImage(){
ProcessDefinition pd = service.getProcessDefinitionByTaskId(taskId);
// 1. 获取流程部署ID
putContext("deploymentId", pd.getDeploymentId());
// 2. 获取流程图片的名称
putContext("imageName", pd.getDiagramResourceName());
// 3.获取当前活动的坐标
Map<String,Object> currentActivityCoordinates =service.getCurrentActivityCoordinates(taskId);
putContext("acs", currentActivityCoordinates);
return "image";
}
其中service.getProcessDefinitionByTaskId(taskId);的代码实现:
public ProcessDefinition getProcessDefinitionByTaskId(String taskId) {
// 1. 得到task
Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
// 2. 通过task对象的pdid获取流程定义对象
ProcessDefinition pd = repositoryService.getProcessDefinition(task.getProcessDefinitionId());
return pd;
}
其中service.getCurrentActivityCoordinates(taskId);的代码实现:
public Map<String, Object> getCurrentActivityCoordinates(String taskId) {
Map<String, Object> coordinates = new HashMap<String, Object>();
// 1. 获取到当前活动的ID
Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
ProcessInstance pi = runtimeService.createProcessInstanceQuery().processInstanceId(task.getProcessInstanceId()).singleResult();
String currentActivitiId = pi.getActivityId();
// 2. 获取到流程定义
ProcessDefinitionEntity pd = (ProcessDefinitionEntity) repositoryService.getProcessDefinition(task.getProcessDefinitionId());
// 3. 使用流程定义通过currentActivitiId得到活动对象
ActivityImpl activity = pd.findActivity(currentActivitiId);
// 4. 获取活动的坐标
coordinates.put("x", activity.getX());
coordinates.put("y", activity.getY());
coordinates.put("width", activity.getWidth());
coordinates.put("height", activity.getHeight());
return coordinates;
}
image页面部分:
从个人任务列表页面点击<a target="_blank" href="/viewCurrentImage?taskId=1" rel="external nofollow" >查看当前流程图</a>跳转到下面页面:
<body>
<!-- 1.获取到规则流程图 这里是用的strust2的标签得到上面上面放入值栈的值-->
<img style="position: absolute;top: 0px;left: 0px;" src="viewImage?deploymentId=<s:property value='#deploymentId'/>&imageName=<s:property value='#imageName'/>">
<!-- 2.根据当前活动的坐标,动态绘制DIV -->
<div style="position: absolute;border:1px solid red;top:<s:property value='#acs.y'/>px;left: <s:property value='#acs.x'/>px;width: <s:property value='#acs.width'/>px;height:<s:property value='#acs.height'/>px; "></div>
</body>
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java超详细讲解抽象类的原理与用法
![](https://img.aspxhome.com/file/2023/6/80306_0s.png)
SpringBoot配置SwaggerUI访问404错误的解决方法
![](https://img.aspxhome.com/file/2023/3/68743_0s.png)
SpringBoot异常处理器的使用与添加员工功能实现流程介绍
![](https://img.aspxhome.com/file/2023/8/60908_0s.png)
浅析Java中如何实现线程之间通信
Java 关于String字符串原理上的问题
浅谈Java的虚拟机结构以及虚拟机内存的优化
![](https://img.aspxhome.com/file/2023/1/64921_0s.jpg)
java实现轻量型http代理服务器示例
Spring自动装配之方法、构造器位置的自动注入操作
Java文件操作实例详解
![](https://img.aspxhome.com/file/2023/2/59562_0s.jpg)
C#设计模式之外观模式介绍
![](https://img.aspxhome.com/file/2023/5/68645_0s.jpg)
SpringMVC中使用@PathVariable绑定路由中的数组的方法
![](https://img.aspxhome.com/file/2023/9/61289_0s.png)
剑指Offer之Java算法习题精讲数组与字符串
![](https://img.aspxhome.com/file/2023/0/77370_0s.png)
详解JAVA类加载机制(推荐)
Java实现SSL双向认证的方法
![](https://img.aspxhome.com/file/2023/0/58090_0s.jpg)
Java源码解析之详解ImmutableMap
![](https://img.aspxhome.com/file/2023/3/59223_0s.png)
java实现文件压缩成zip的工具类
8种常见的Java不规范代码
![](https://img.aspxhome.com/file/2023/5/70335_0s.jpg)
Mybatis 缓存原理及失效情况解析
![](https://img.aspxhome.com/file/2023/6/82996_0s.jpg)
Spring占位符Placeholder的实现原理解析
![](https://img.aspxhome.com/file/2023/5/66075_0s.png)