java - 如何在Spring Boot Restful应用程序中响应pbf文件?

标签 java spring-boot content-type protobuf-java

我有一个 pbf 文件,如 this我想用 spring boot 2.2.2.RELEASE 来回应它完整的应用程序。 Mapbox响应服务器如下,我想用spring boot实现它:

Mapbox response headers

Mapbox Content-Type 是 application/x-protobuf,我怎样才能在 Spring Boot Restfull 应用程序中做到这一点?

最佳答案

可以这样下载

@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);
    }
}

关于java - 如何在Spring Boot Restful应用程序中响应pbf文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59465862/

相关文章:

spring-security - 使用Spring Security Java配置时禁用基本身份验证

php - 在 CodeIgniter 输出中使用 Exit 会使内容类型 header 变为 text/html 而不是定义的 header

java - UrlConnection 无内容类型

java - 3sum 解决方案未通过测试用例(Java)

java - Spring Batch - JpaPagingItemReader - 在 MySQL 中工作 - 在 PostgreSQL 中重复

Java 和 Heritrix 3.1.x : Web Content parsing?

java - spring boot sql数据库DML和DDL脚本

java - 在 Kotlin 注释参数中使用 java 常量

python - 如何使用 cgi python 脚本在浏览器中显示 pdf 文件内容及其全名?

java - Java 中默认构造函数的可见性