我用的是spring+springmvc+mybatis,前端使用的是jsp
===========前台jsp中的写法==========
----标签直接定义一个图片,定义一个id
js的下法如下:
var src = "${pageContext.request.contextPath}/usertask/imgs.do?cacheFlag"+new Date().getTime();
$("#imgObj").attr("src", src);
提示:你可以定义一个按钮,然后点击按钮就会执行一个方法,将上面两行直接放在方法中就行
===========后台中的写法==========
==========controller的写法============
/**
* 获取流程跟踪图
* @param req
* @param res
*/
@RequestMapping("/imgs")
public void getCode(HttpServletRequest req, HttpServletResponse res){
InputStream in = null;
byte[] b=new byte[1024];
try {
ServletOutputStream sos = res.getOutputStream();
in = userTaskService.imgs();
//文件名
String src3="UserTask.png";
res.setHeader("Content-Disposition","attachment;filename="+URLEncoder
.encode(src3,"UTF-8"));
int i=in.read(b, 0, b.length);
while(i!=-1){
sos.write(b, 0, i);
i=in.read(b,0,b.length);
}
in.close();
sos.close();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
==========service的写法============
/**
* 流程图跟踪
* @return
*/
public InputStream imgs(){
String taskId="16001";//这里的任务id应该从前台传过来,可以直接在数据库里查一个放在这里先试试
//processInstanceId
String processInstanceId = taskService.createTaskQuery().taskId(taskId).singleResult().getProcessInstanceId();
//获取历史流程实例
HistoricProcessInstance processInstance = historyService.createHistoricProcessInstanceQuery().processInstanceId(processInstanceId).singleResult();
//获取流程图
BpmnModel bpmnModel = repositoryService.getBpmnModel(processInstance.getProcessDefinitionId());
ProcessEngineConfiguration processEngineConfiguration = processEngine.getProcessEngineConfiguration();
Context.setProcessEngineConfiguration((ProcessEngineConfigurationImpl) processEngineConfiguration);
ProcessDiagramGenerator diagramGenerator = processEngineConfiguration.getProcessDiagramGenerator();
ProcessDefinitionEntity definitionEntity = (ProcessDefinitionEntity)repositoryService.getProcessDefinition(processInstance.getProcessDefinitionId());
List
//高亮环节id集合
List
//高亮线路id集合
List
for(HistoricActivityInstance tempActivity : highLightedActivitList){
String activityId = tempActivity.getActivityId();
highLightedActivitis.add(activityId);
}
//中文显示的是口口口,设置字体就好了
InputStream imageStream = diagramGenerator.generateDiagram(bpmnModel, "png", highLightedActivitis,highLightedFlows,"宋体","宋体","宋体",null,1.0);
//单独返回流程图,不高亮显示
return imageStream;
}
/**
* 获取需要高亮的线
* @param processDefinitionEntity
* @param historicActivityInstances
* @return
*/
private List
ProcessDefinitionEntity processDefinitionEntity,
List
List
for (int i = 0; i
.findActivity(historicActivityInstances.get(i)
.getActivityId());// 得到节点定义的详细信息
List
ActivityImpl sameActivityImpl1 = processDefinitionEntity
.findActivity(historicActivityInstances.get(i + 1)
.getActivityId());
// 将后面第一个节点放在时间相同节点的集合里
sameStartTimeNodes.add(sameActivityImpl1);
for (int j = i + 1; j
.get(j);// 后续第一个节点
HistoricActivityInstance activityImpl2 = historicActivityInstances
.get(j + 1);// 后续第二个节点
if (activityImpl1.getStartTime().equals(
activityImpl2.getStartTime())) {
// 如果第一个节点和第二个节点开始时间相同保存
ActivityImpl sameActivityImpl2 = processDefinitionEntity
.findActivity(activityImpl2.getActivityId());
sameStartTimeNodes.add(sameActivityImpl2);
} else {
// 有不相同跳出循环
break;
}
}
List
.getOutgoingTransitions();// 取出节点的所有出去的线
for (PvmTransition pvmTransition : pvmTransitions) {
// 对所有的线进行遍历
ActivityImpl pvmActivityImpl = (ActivityImpl) pvmTransition
.getDestination();
// 如果取出的线的目标节点存在时间相同的节点里,保存该线的id,进行高亮显示
if (sameStartTimeNodes.contains(pvmActivityImpl)) {
highFlows.add(pvmTransition.getId());
}
}
}
return highFlows;
}
===============================================
下面是我的效果图