我正在尝试发送一个文件到响应和支持编码。代码主要是从Netty github中的一个示例复制而来的。
ChannelFuture flushFuture;
if (context.pipeline().get(SslHandler.class) == null) {
// SSL not enabled - can use zero-copy file transfer.
context.write(new DefaultFileRegion(raf.getChannel(), 0, getLength()), context.newProgressivePromise());
flushFuture = context.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);
} else {
// SSL enabled - cannot use zero-copy file transfer.
try {
// HttpChunkedInput will write the end marker (LastHttpContent) for us.
flushFuture = context.writeAndFlush(new HttpChunkedInput(new ChunkedFile(raf, 0, getLength(), 8192)),
context.newProgressivePromise());
} catch (IOException e) {
throw new CustomizableResponseTypeException("Could not read file", e);
}
}当服务器使用SSL时,一切工作正常。当没有使用SSL时,零拷贝文件传输正在进行,编码不会产生正确的输出。
我已经准备好了这个blog post,它似乎表明我正在尝试做的事情可以工作,但是我不明白示例代码的哪一部分可以使它工作。任何提示或帮助都将不胜感激。
谢谢!
发布于 2018-03-09 00:55:07
这似乎是不可能的。解决方案是仅对较小或不可压缩的文件使用零拷贝。
https://stackoverflow.com/questions/49162206
复制相似问题