[英]Netty HttpStaticFileServer example not working with HttpContentCompressor
如果我将以下行添加到HttpStaticFileServerInitializer
pipeline.addLast("decoder", new HttpRequestDecoder());
pipeline.addLast("aggregator", new HttpObjectAggregator(65536));
pipeline.addLast("encoder", new HttpResponseEncoder());
pipeline.addLast("chunkedWriter", new ChunkedWriteHandler());
pipeline.addLast("deflater", new HttpContentCompressor()); // Added
这些文件通过Content-Encoding: gzip
但内容实际上并未进行gzip压缩。 这会导致大多数浏览器无法解码内容。
DefaultFileRegion
不能与HttpContentCompressor
,或者还有什么必须要让它们一起工作?
您可以获得分块传输(ChunkedWriteHandler)和HttpContentCompressor一起工作。 您只需提供一个ChunkedInput来生成HttpContent对象而不是ByteBuf对象。
这是一个快速的写作: http : //andreas.haufler.info/2014/01/making-http-content-compression-work-in.html
不幸的是,分块传输和gzip内容编码是一个困难的组合。 压缩不能在块级别上进行,而是在整个内容上进行(使得实时数据不可能)。 因此,您需要自己压缩内容然后传输它。 在您的示例中,这意味着您不能组合使用块编写器和内容压缩器。
根据Clement的评论以及随后在#netty中的讨论,听起来似乎不可能同时使用DefaultFileRegion
和HttpContentCompressor
。 我切换回ChunkedFile
,它工作正常。
RandomAccessFile raf = new RandomAccessFile(file, "r");
if (request.method() != HttpMethod.HEAD) {
ctx.write(new ChunkedFile(raf, 0, fileLength, 8192));
}
我还对HttpContentCompressor
进行了子类化,并对内容类型进行了一些检查,以确定文件是否应该被压缩(JPEG,PNG和其他二进制文件跳过压缩步骤。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.