最近在學習springboot+activiti7整合,想做一個導出bpmn文件的功能,查了相關資料,最后沒有實現。最后查看了一下代碼 找到了方法 如下所示
@GetMapping("export")
@Transactional
public void export(@RequestParam("definitionId") String processInstanceId, HttpServletResponse response) {
BufferedOutputStream bos = null;
try {
try {
RepositoryService repositoryService = ProcessEngines.getDefaultProcessEngine().getRepositoryService();
//
ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().processDefinitionId(processInstanceId).singleResult();
InputStream inputStream = repositoryService
.getProcessModel(processInstanceId);
ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
byte[] bpmnBytes = new byte[1024]; //buff用於存放循環讀取的臨時數據
int rc = 0;
while ((rc = inputStream.read(bpmnBytes, 0, 100)) > 0) {
swapStream.write(bpmnBytes, 0, rc);
}
byte[] in_b = swapStream.toByteArray(); //in_b為轉換之后的結果
// 封裝輸出流
bos = new BufferedOutputStream(response.getOutputStream());
bos.write(in_b);// 寫入流
String filename = processDefinition.getName() + ".bpmn";
response.setContentType("application/x-msdownload;");
response.setHeader("Content-Disposition",
"attachment; filename=" + filename);
response.flushBuffer();
} finally {
bos.flush();
bos.close();
}
} catch (Exception e) {
System.out.println("導出文件失敗");
e.printStackTrace();
}
}
其中
InputStream inputStream = repositoryService
.getProcessModel(processInstanceId); 可以獲取bpmn文件流 直接操作導出即可