首页 > 解决方案 > 在 Flutter 应用程序中消耗 Spring Boot 资源?(响应对象字节数组到文件)

问题描述

我有一个 Spring Boot 后端,它为我提供文件(在本例中为 jpeg 文件)。

这是弹簧休息控制器的样子:

    @Override
    public ResponseEntity<Resource> get(Long id) {
        MyCustomFile entity = service.get(id);

        if (entity == null) {
            return ResponseEntity.notFound().build();
        }

        return new ResponseEntity<>(toResource(entity), toHeaders(entity), HttpStatus.OK);
    }

    protected Resource toResource(MyCustomFile myfile) {
        return toResource(myfile.getContent()); 
              // here the getContent() returns array of bytes: private byte[] content;
    }

    protected Resource toResource(byte[] data) {
        return new ByteArrayResource(data);
    }

但是如何在我的颤振应用程序中使用它呢?目前我有这个:

  getFileById(int id, String token) async {
    var response = await http.get(Uri.parse("$URL$id"), headers: {
      HttpHeaders.authorizationHeader: 'bearer $token',
    });
  }

我有这个response对象,但我不知道如何变成一个文件以便我可以处理它......(例如将它保存到手机......)

当我打印response.body

在此处输入图像描述

标签: spring-bootflutterdart

解决方案


推荐阅读