我正在尝试实现一个 HTTP 服务器(使用 Netty),它不仅可以提供“常规”html 页面,还可以提供大文件。因此,我想在我的管道中使用 ChunkedWriteHandler
以及 HttpContentCompressor
。
目前,这个管道初始化如下:
pipeline.addLast("decoder", new HttpRequestDecoder());
pipeline.addLast("aggregator", new HttpObjectAggregator(1048576));
pipeline.addLast("encoder", new HttpResponseEncoder());
pipeline.addLast("chunkedWriter", new ChunkedWriteHandler());
pipeline.addLast("deflater", new HttpContentCompressor());
pipeline.addLast(new NettyHandler());
NettyHandler
遵循以下方案:
@Override
public void channelRead(final ChannelHandlerContext context, final Object message) throws Exception {
try {
if (message instanceof HttpRequest) {
final HttpRequest request = (HttpRequest) message;
final HttpContext httpContext = new HttpContext(request, context);
final ChannelFuture future = handleHttpMessage(httpContext);
httpContext.closeOn(future);
}
} finally {
ReferenceCountUtil.release(message);
}
}
private ChannelFuture handleHttpMessage(final HttpContext context) {
//writing to the wire via ChannelHandlerContext.write(...)
return context.getChannelContext().writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);
}
如果我请求/发送小文件(我的测试文件大约 500 字节),一切正常。但是一旦请求的文件变大(我的测试文件大约 350 MB),浏览器(使用 chrome 和 firefox 进行测试)报告有关接收到的主体的编码部分的问题。 chrome 说 ERR_CONTENT_DECODING_FAILED
,firefox 说类似无法读取源文件
。
我做错了什么吗?我是否必须即时操纵管道?在此先感谢您的帮助!
最佳答案
您需要将写入的 block 包装到 DefaultHttpContent 中,因为 HttpContentCompressor 不理解 ByteBuf 实例。
因此只需将一个特殊的 HttpContentCompressor 放入知道如何处理 ByteBuf 实例的 ChannelPipeline 中。类似于 HttpChunkContentCompressor在 vert.x 项目中。
确保将它放在 ChunkedWriteHandler 之前。
关于java - Netty - 如何将 ChunkedWriteHandler 与 HttpContentCompressor 结合起来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18982925/