Spring Boot Restful应用程序如何响应pbf文件?

问题描述 投票:0回答:1

我有一个像this这样的pbf文件,我想用spring boot 2.2.2.RELEASE resfull应用程序进行响应。 Mapbox响应服务器如下,我想通过Spring Boot来实现它:

Mapbox response headers

Mapbox内容类型为application/x-protobuf,如何在Spring Boot Restfull应用程序中做到这一点?

java spring-boot content-type protobuf-java
1个回答
0
投票

您可以这样下载

@RestController
@RequestMapping("/api")
public class DownloadsController {

    @GetMapping("/getFile")
    public ResponseEntity<Resource> getFile(HttpServletResponse httpServletResponse) throws FileNotFoundException{
        File file = new File("/home/sagara/Downloads/3.vector.pbf");
        HttpHeaders headers = new HttpHeaders();
        String fileName = file.getName();
        headers.add("Content-disposition","attachment; filename="+ fileName);
        Resource resource = new InputStreamResource(new FileInputStream(file));
        return ResponseEntity.ok()
            .headers(headers)
            .contentLength(file.length())
            .contentType(MediaType.parseMediaType("application/x-protobuf"))
            .body(resource);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.