HttpResponse response = new DefaultHttpResponse(response.protocolVersion(), response.status());
HttpHeaders headers = response.headers();
// 设置transfer_encoding
headers.set(HttpHeaderNames.TRANSFER_ENCODING, HttpHeaderValues.CHUNKED);
ctx.write(response);
// 然后输出真实数据, 这里采取读取文件
try (FileInputStream fis = new FileInputStream(file)) {
byte[] bytes = new byte[BUFFER_SIZE];
for (int offset = fis.read(bytes, 0, BUFFER_SIZE); offset > 0;
offset = fis.read(bytes, 0, BUFFER_SIZE)) {
ctx.write(new DefaultHttpContent(Unpooled.wrappedBuffer(bytes, offset, len)));
}
} catch (IOException e) {
throw new RuntimeException(e);
}
// 最后输出空
ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);