我想压缩javax.servlet.Filter中的响应主体。这是我的代码
byte[] bytes = // compressing response body
response.addHeader("Content-Encoding", "gzip");
response.addHeader("Content-Length", String.valueOf(bytes.length));
response.setContentLength(bytes.length);
response.setBufferSize(bytes.length * 2);
ServletOutputStream output = response.getOutputStream();
output.write(bytes);
output.flush();
output.close();
但是我在Chrome开发工具中看到的实际响应是
Accept-Ranges: bytes
Cache-Control: max-age=2592000
Content-Type: application/javascript;charset=UTF-8
Date: Fri, 14 Dec 2018 15:34:25 GMT
Last-Modified: Tue, 09 Oct 2018 13:42:54 GMT
Server: Apache-Coyote/1.1
Transfer-Encoding: chunked
我不希望Transfer-Encoding:分块,因为我声明了“ Content-Length”。我在Java上写了一个简单的测试
URLConnection connection = new URL("http://127.0.0.1:8081/js/ads.js").openConnection();
connection.addRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
connection.addRequestProperty("Accept-Encoding", "gzip, deflate");
connection.addRequestProperty("Accept-Language", "ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7");
connection.addRequestProperty("Cache-Control", "no-cache");
connection.addRequestProperty("Connection", "keep-alive");
connection.addRequestProperty("Host", "127.0.0.1:8081");
connection.addRequestProperty("Pragma", "no-cache");
connection.addRequestProperty("Upgrade-Insecure-Requests", "1");
connection.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36");
connection.connect();
connection.getHeaderFields().forEach((s, strings) ->
System.out.println(s + ":" + String.join(",", strings)));
这就是我发现的:
我调试了sun.nio.ch.SocketChannel#write方法,它使用Content-Length标头值获取了正确的ByteBuffer。
我无法理解这种神奇的变身在哪里发生?
更新
奇怪的是,我将gziped字节写入Socket(我确定在调试时可以调用SocketChannel实现中的本机方法write)。但是,如果我未指定User-Agent标头或放置一些随机字符串,URLConnection将返回带有Chrome的User-Agent的未压缩字节数组和正确的gziped字节数组。 因此,似乎在Windows套接字实现中的某处发生了魔术。
答案 0 :(得分:0)
显示的代码
我认为您显示的代码有效并且问题出在其他地方。
设置
测试用例
我试图创建一个小的过滤器示例,以便能够测试您的测试代码。
顺便说一句,在 Tomcat附带的示例(webapps \ examples \ WEB-INF \ classes \ compressionFilters)。
import java.io.*;
import java.util.zip.GZIPOutputStream;
import javax.servlet.*;
import javax.servlet.http.*;
public class CompressionFilter implements Filter {
public void init(FilterConfig filterConfig) { }
public void destroy() { }
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
final HttpServletRequest request = (HttpServletRequest) servletRequest;
final HttpServletResponse response = (HttpServletResponse) servletResponse;
ResponseWrapper wrapper = new ResponseWrapper(response);
filterChain.doFilter(request, wrapper);
byte[] uncompressed = wrapper.getBytes();
byte[] bytes = compress(uncompressed);
response.addHeader("Content-Encoding", "gzip");
response.addHeader("Content-Length", String.valueOf(bytes.length));
response.setContentLength(bytes.length);
//response.setBufferSize(bytes.length * 2);
ServletOutputStream output = response.getOutputStream();
output.write(bytes);
output.flush();
output.close();
System.out.println("request to:" + request.getServletPath()
+ " size changed from: " + uncompressed.length
+ " to " + bytes.length);
}
private byte[] compress(byte[] bytes) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
GZIPOutputStream gzipOutputStream = new GZIPOutputStream(baos);
gzipOutputStream.write(bytes);
gzipOutputStream.close();
return baos.toByteArray();
}
public class ResponseWrapper extends HttpServletResponseWrapper {
private ByteArrayOutputStream output = new ByteArrayOutputStream();
private PrintWriter printWriter = null;
ResponseWrapper(HttpServletResponse response) {
super(response);
}
byte[] getBytes() {
if (printWriter != null)
printWriter.flush();
return output.toByteArray();
}
public PrintWriter getWriter() {
if (printWriter == null)
printWriter = new PrintWriter(output);
return printWriter;
}
public ServletOutputStream getOutputStream() {
return new ServletOutputStream() {
private WriteListener writeListener;
public boolean isReady() { return true; }
public void setWriteListener(WriteListener writeListener) { this.writeListener = writeListener; }
public void write(int b) {
output.write(b);
if(writeListener != null)
writeListener.notify();
}
};
}
}
}
结果
Chrome的开发人员工具中显示了三个带有静态html的测试用例,一个JSP生成的页面和一个带有一些虚拟内容的Servlet生成的页面:
a)使用静态html
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Accept-Ranges: bytes
ETag: W/"108-1545775482914"
Last-Modified: Tue, 25 Dec 2018 22:04:42 GMT
Content-Encoding: gzip
Content-Type: text/html
Content-Length: 97
Date: Tue, 25 Dec 2018 22:34:41 GMT
b)生成了JSP
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Encoding: gzip
Content-Type: text/html
Content-Length: 38
Date: Tue, 25 Dec 2018 22:49:17 GMT
c)生成Servlet
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Encoding: gzip
Content-Type: text/html
Content-Length: 65
Date: Tue, 25 Dec 2018 22:49:43 GMT
通过此设置,没有 Transfer-Encoding:分块。那么也许可以在其他地方找到此分块标题的原因吗?