如何使用spring webflux进行文件流传输

shm*_*nik 6 spring-webflux

我想使用 spring webflux 以反应方式流式传输文件。
我的端点应该看起来更具体,对象的类型是什么?

@GetMapping("/file")
Flux<???> file() {
    //Read file content into this ??? thing . 
}
Run Code Online (Sandbox Code Playgroud)

Bri*_*zel 5

您可以返回Resource这样的实例:

@GetMapping("/file")
Mono<Resource> file() {
    //Create a ClassPathResource, for example
}
Run Code Online (Sandbox Code Playgroud)

请注意,这会自动支持字节范围 HTTP 请求。