我正在尝试实现一个 HTTP 服务器(使用 Netty),它不仅提供“常规”html 页面,还提供大文件。因此,我想在我的管道中使用 theChunkedWriteHandler
和 the 。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 说类似source file could not be read
.
我在做一些根本错误的事情吗?我必须即时操作管道吗?在此先感谢您的帮助!