如果我将下面一行添加到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一起使用,但内容实际上并不是压缩的。这导致大多数浏览器无法对内容进行解码。
DefaultFileRegion不是与HttpContentCompressor一起工作吗?或者,要让他们一起工作,还必须做些什么?
发布于 2014-01-07 22:12:31
您可以让分块传输(ChunkedWriteHandler)和HttpContentCompressor一起工作。您只需提供一个ChunkedInput来生成HttpContent对象,而不是ByteBuf对象。
下面是一个快速编写:http://andreas.haufler.info/2014/01/making-http-content-compression-work-in.html
发布于 2013-11-24 10:13:08
不幸的是,分块传输和gzip内容编码是一个困难的组合。压缩不能在块级进行,而必须在整个内容上进行(使活数据无法进行)。因此,您需要自己压缩内容,然后传输它。在您的示例中,它意味着不能组合使用块编写器和内容压缩器。
发布于 2013-12-09 13:40:33
基于克莱门特的评论和随后在#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和其他二进制文件跳过压缩步骤)。
https://stackoverflow.com/questions/20136334
复制相似问题