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

叁歲伎倆 2022-01-22 07:55 1126阅读 0赞

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

  • 服务提供者流接口

    @RequestMapping(value = “feginProcessDiagram”, method = RequestMethod.GET, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public void genFeginProcessDiagram(HttpServletResponse httpServletResponse, String processId) throws Exception {

    1. ProcessInstance pi = runtimeService.createProcessInstanceQuery().processInstanceId(processId).singleResult();
    2. //流程走完的不显示图
    3. if (pi == null) {
    4. return;
    5. }
    6. Task task = taskService.createTaskQuery().processInstanceId(pi.getId()).singleResult();
    7. //使用流程实例ID,查询正在执行的执行对象表,返回流程实例对象
    8. String InstanceId = task.getProcessInstanceId();
    9. List < Execution > executions = runtimeService.createExecutionQuery().processInstanceId(InstanceId).list();
    10. //得到正在执行的Activity的Id
    11. List < String > activityIds = new ArrayList < > ();
    12. List < String > flows = new ArrayList < > ();
    13. for (Execution exe: executions) {
    14. List < String > ids = runtimeService.getActiveActivityIds(exe.getId());
    15. activityIds.addAll(ids);
    16. }
    17. //获取流程图
    18. BpmnModel bpmnModel = repositoryService.getBpmnModel(pi.getProcessDefinitionId());
    19. ProcessEngineConfiguration engconf = processEngine.getProcessEngineConfiguration();
    20. ProcessDiagramGenerator diagramGenerator = engconf.getProcessDiagramGenerator();
    21. InputStream in = diagramGenerator.generateDiagram(bpmnModel, "png", activityIds, flows, engconf.getActivityFontName(),
    22. engconf.getLabelFontName(), engconf.getAnnotationFontName(), engconf.getClassLoader(), 1.0, true);
    23. OutputStream out = null;
    24. byte[] buf = new byte[1024];
    25. int legth = 0;
    26. try {
    27. out = httpServletResponse.getOutputStream();
    28. while ((legth = in .read(buf)) != -1) {
    29. out.write(buf, 0, legth);
    30. }
    31. } finally {
    32. if ( in != null) {
    33. in .close();
    34. }
    35. if (out != null) {
    36. out.close();
    37. }
    38. }

    }

  • 服务提供者远程调用接口

    @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 {

    1. ResponseEntity<Resource> entity = restTemplate.postForEntity("http://microservice-flowable/expense/ribbonProcessDiagram/{processId}", processId, Resource.class,processId);
    2. InputStream in = entity.getBody().getInputStream();
    3. System.out.println(in);
    4. OutputStream out = null;
    5. byte[] buf = new byte[1024];
    6. int legth = 0;
    7. try {
    8. out = httpServletResponse.getOutputStream();
    9. while ((legth = in.read(buf)) != -1) {
    10. out.write(buf, 0, legth);
    11. }
    12. } finally {
    13. if (in != null) {
    14. in.close();
    15. }
    16. if (out != null) {
    17. out.close();
    18. }
    19. }

    }

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

参考:

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

RestTemplate响应中获取输入流InputStream

发表评论

表情:
评论列表 (有 0 条评论,1126人围观)

还没有评论,来说两句吧...

相关阅读