我如何知道HTTP响应的压缩前后大小?

时间:2019-07-26 11:20:38

标签: java http-headers http-content-length spring-remoting gzipoutputstream

我正在使用自己的自定义类,该类扩展了Spring的HttpInvokerServiceExporter类,以接受来自客户端的请求并发送回响应。 在我的情况下,客户端不是浏览器,而是基于SWT的应用程序,用于向Websphere Application Server发送响应或从Websphere Application Server接收响应。

我需要先压缩响应,然后再将其发送回客户端。由于我的课程正在扩展HttpInvokerServiceExporter类,因此已经重写了handleRequest(最终的HttpServletRequest请求,最终的HttpServletResponse响应)函数。我必须重写函数decorateOutputStream(HttpServletRequest请求,HttpServletResponse响应,OutputStream os)来实现压缩。

在decorateOutputStream函数中,我将HttpHeaders.CONTENT_ENCODING的值设置为“ gzip”,并将HttpHeaders.CONTENT_TYPE的值设置为“ binary / gzip”。下面是我的函数decorateOutputStream。

public class MyInvokerServicesExporter extends HttpInvokerServiceExporter {

@Override
public void handleRequest(final HttpServletRequest request, final HttpServletResponse response)
      throws IOException, ServletException {
.....
}

@Override
protected void writeRemoteInvocationResult(HttpServletRequest request, HttpServletResponse response, RemoteInvocationResult result, OutputStream os) throws IOException {

    ObjectOutputStream oos = createObjectOutputStream(decorateOutputStream(request, response, os));
    try {
      doWriteRemoteInvocationResult(result, oos);
    } finally {
      oos.close();
    }
  }

@Override
protected OutputStream decorateOutputStream(HttpServletRequest request, HttpServletResponse response, OutputStream os) throws IOException {
    if (GZIP.equals(request.getHeader(REQ_HEADER_ACCEPT_ENC))) {
      response.addHeader(HttpHeaders.CONTENT_ENCODING, GZIP);
      response.addHeader(HttpHeaders.CONTENT_TYPE, RES_HEADER_CONTENT_TYPE);
      return new GZIPOutputStream(os);
    } else {
      return os;
    }
  }
}

此代码运行正常。我想知道服务器上响应后压缩的大小。我怎么能得到它?

0 个答案:

没有答案