@GetMapping(value = "/download")
public Flux<DataBuffer> flux(ServerHttpResponse response) throws IOException {
final Stream<Integer> stream = Stream.iterate(1, i -> i + 1).limit(1000);
String fileName = String.format("%s.csv", RandomStringUtils.randomAlphabetic(10));
response.getHeaders().set(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + fileName);
response.getHeaders().setContentType(MediaType.APPLICATION_OCTET_STREAM);
final Flux<DataBuffer> next = Flux.<DataBuffer>create(emitter -> {
stream
.map(i -> String.format("%d", i).getBytes(StandardCharsets.UTF_8))
.forEach(bytes -> {
final DefaultDataBuffer dataBuffer = new DefaultDataBufferFactory().allocateBuffer();
final OutputStream outputStream = dataBuffer.asOutputStream();
try {
outputStream.write(Byte.decode("DD"));
outputStream.flush();
emitter.next(dataBuffer);
} catch (IOException e) {
e.printStackTrace();
}
});
emitter.complete();
}
);
return next;
}
时emiiter.next(dataBuffer)
,我无法从 webflux 服务器获得响应。
什么时候emitter.complete()
,我得到一个总的回应。我想实时获得流媒体,我该怎么做?