1. 环境
kotlin + webflux
gradle依赖
implementation("org.springframework.boot:spring-boot-starter-webflux")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-reactor")
2. 从文件中获取数据并返回文件流
@GetMapping("/download")
fun download( response: ServerHttpResponse): Mono<Void> {
//数据准备
val file = File("./test.txt")
file.writeText("123")
try {
val input = FileInputStream(file)
val dataBufferFlux: Flux<DataBuffer> = DataBufferUtils.readByteChannel(input::getChannel,DefaultDataBufferFactory(),4096)
response.headers.set(HttpHeaders.CONTENT_DISPOSITION,"attachment;filename=test.txt");
response.headers.contentType = MediaType("application"