Activiti应用实践(五)-查询BPMN XML文件和图片资源文件

一、概述

Activiti流程部署后,前端也需要在页面上看到流程的定义,还有可能会修改。下面给的是获取图片资源文件和XML文件的样例代码。

二、获取图片资源

Activiti提供了经由BPMN XML文件生成png图片文件的功能,也有其它更强的能力。但需要解决不同环境,字体、乱码等问题。如果需求比较简单,只是需要静态查看一下流程的定义,倒是可以在部署时直接提供一张图片,后面再查询该图片返回给前端展示。

1、rest层接口代码

/**
 * 获取流程定义图片。
 *
 * @param response http请求响应
 * @param processDefineId 流程定义ID
 */
@GetMapping("/v1/process-image")
@ApiOperation(value = "获取流程定义图片")
public void getProcessDefineImge(HttpServletResponse response,
                                 @ApiParam(value = "流程定义ID")
                                 @RequestParam("processDefineId") String processDefineId){
    InputStream inputStream = activitiProcessService.getProcessDefineResource(processDefineId, 2);

    byte[] bytes = new byte[1024];
    response.setContentType("image/png");

    try {
        OutputStream outputStream = response.getOutputStream();
        while (inputStream.read(bytes) != -1) {
            outputStream.write(bytes);
        }

        inputStream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

2、业务层实现逻辑

/**
 * 获取流程定义资源。
 *
 * @param processDefineId 流程定义ID
 * @param type 1-获取xml, 2-获取图片
 * @return 文件流
 */
public InputStream getProcessDefineResource(String processDefineId, int type) {
    ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
            .processDefinitionId(processDefineId).singleResult();

    String resourceName = "";
    if (type == 1) {
        resourceName = processDefinition.getResourceName();
    } else if (type == 2) {
        resourceName = processDefinition.getDiagramResourceName();
    } else {
        return null;
    }

    return repositoryService.getResourceAsStream(processDefinition.getDeploymentId(), resourceName);
}

三、获取BPMN XML文件

1、rest层接口

/**
 * 获取流程定义XML。
 *
 * @param response http请求响应
 * @param processDefineId 流程定义ID
 */
@GetMapping("/v1/process-xml")
@ApiOperation(value = "获取流程定义XML")
public void getProcessDefineXML(HttpServletResponse response,
                                 @ApiParam(value = "流程定义ID")
                                 @RequestParam("processDefineId") String processDefineId){
    InputStream inputStream = activitiProcessService.getProcessDefineResource(processDefineId, 1);

    byte[] bytes = new byte[1024];
    response.setContentType("text/xml");

    try {
        OutputStream outputStream = response.getOutputStream();
        while (inputStream.read(bytes) != -1) {
            outputStream.write(bytes);
        }

        inputStream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

2、业务层实现逻辑

与获取图片资源接口共用。

源码GitHub路径: https://github.com/ylforever/elon-activiti

发布了113 篇原创文章 · 获赞 183 · 访问量 29万+

猜你喜欢

转载自blog.csdn.net/ylforever/article/details/99708863