Feign实现微服务间调用返回stream

今天来讲述一下fegin的调用返回stream,得到stream我们可以下载,写入到页面展示图片等;
我们就开始讲述一下:

  • 服务提供者流接口
@RequestMapping(value = "feginProcessDiagram", method = RequestMethod.GET, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
public void genFeginProcessDiagram(HttpServletResponse httpServletResponse, String processId) throws Exception {
    ProcessInstance pi = runtimeService.createProcessInstanceQuery().processInstanceId(processId).singleResult();
    //流程走完的不显示图
    if (pi == null) {
        return;
    }
    Task task = taskService.createTaskQuery().processInstanceId(pi.getId()).singleResult();
    //使用流程实例ID,查询正在执行的执行对象表,返回流程实例对象
    String InstanceId = task.getProcessInstanceId();
    List < Execution > executions = runtimeService.createExecutionQuery().processInstanceId(InstanceId).list();
    //得到正在执行的Activity的Id
    List < String > activityIds = new ArrayList < > ();
    List < String > flows = new ArrayList < > ();
    for (Execution exe: executions) {
        List < String > ids = runtimeService.getActiveActivityIds(exe.getId());
        activityIds.addAll(ids);
    }
    //获取流程图
    BpmnModel bpmnModel = repositoryService.getBpmnModel(pi.getProcessDefinitionId());
    ProcessEngineConfiguration engconf = processEngine.getProcessEngineConfiguration();
    ProcessDiagramGenerator diagramGenerator = engconf.getProcessDiagramGenerator();
    InputStream in = diagramGenerator.generateDiagram(bpmnModel, "png", activityIds, flows, engconf.getActivityFontName(),
        engconf.getLabelFontName(), engconf.getAnnotationFontName(), engconf.getClassLoader(), 1.0, true);
    OutputStream out = null;
    byte[] buf = new byte[1024];
    int legth = 0;
    try {
        out = httpServletResponse.getOutputStream();
        while ((legth = in .read(buf)) != -1) {
            out.write(buf, 0, legth);
        }
    } finally {
        if ( in != null) {
            in .close();
        }
        if (out != null) {
            out.close();
        }
    }
}
  • 服务提供者远程调用接口
@RequestMapping(value = "/expense/feginProcessDiagram",method = RequestMethod.GET,consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
Response genFeginProcessDiagram(@RequestParam("httpServletResponse") HttpServletResponse httpServletResponse, @RequestParam("processId") String processId) throws Exception;

用feign.Response来接收

  • 服务提供者下载文件接口
@RequestMapping(value = "ribbonProcessDiagram/{processId}")
public void genProcessDiagram1(HttpServletResponse httpServletResponse, @PathVariable String processId) throws Exception {
    ResponseEntity<Resource> entity = restTemplate.postForEntity("http://microservice-flowable/expense/ribbonProcessDiagram/{processId}", processId, Resource.class,processId);
    InputStream in = entity.getBody().getInputStream();
    System.out.println(in);

    OutputStream out = null;
    byte[] buf = new byte[1024];
    int legth = 0;
    try {
      out = httpServletResponse.getOutputStream();
      while ((legth = in.read(buf)) != -1) {
        out.write(buf, 0, legth);
      }
    } finally {
      if (in != null) {
        in.close();
      }
      if (out != null) {
        out.close();
      }
    }
}

只要我们拿到InputStream然后我们就可以操作了,下载,写入直接展示图片!

参考:

https://blog.csdn.net/aaronsimon/article/details/82710979

RestTemplate响应中获取输入流InputStream

发布了112 篇原创文章 · 获赞 87 · 访问量 17万+

猜你喜欢

转载自blog.csdn.net/zhaokejin521/article/details/90905138