代碼參考 閑鹿群友@
自定義的ProcessController類裡,判斷流程是否已執行結束,執行結束 則查詢曆史流程
@RequestMapping(value = "/read-resource")
public void readResource(String pProcessInstanceId, HttpServletResponse response)
throws Exception {
// 設定頁面不緩存
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
ProcessDefinitionQuery pdq = repositoryService.createProcessDefinitionQuery();
//運作中的流程
ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceId(pProcessInstanceId).singleResult();
ProcessDefinition pd;
if(processInstance == null){
//曆史流程/已結束的流程 比較複雜的流程圖不建議顯示執行流程,高亮線條流程等 看起來會比較亂
HistoricProcessInstance historicProcessInstance = historyService.createHistoricProcessInstanceQuery().processInstanceId(pProcessInstanceId).singleResult();
//已結束流程查詢
pd = pdq.processDefinitionId(historicProcessInstance.getProcessDefinitionId()).singleResult();
}else{
//正在執行的流程查詢
pd = pdq.processDefinitionId(processInstance.getProcessDefinitionId()).singleResult();
}
String resourceName = pd.getDiagramResourceName();
if(resourceName.endsWith(".png") && StringUtils.isEmpty(pProcessInstanceId) == false)
{
getActivitiProccessImage(pProcessInstanceId,response);
//ProcessDiagramGenerator.generateDiagram(pde, "png", getRuntimeService().getActiveActivityIds(processInstanceId));
}
else
{
// 通過接口讀取
InputStream resourceAsStream = repositoryService.getResourceAsStream(pd.getDeploymentId(), resourceName);
// 輸出資源内容到相應對象
byte[] b = new byte[1024];
int len = -1;
while ((len = resourceAsStream.read(b, 0, 1024)) != -1) {
response.getOutputStream().write(b, 0, len);
}
}
}