我正在使用拦截器,它会在请求之前和之后拦截。这样做的目的是获取发送和返回的内容的大小,但是,我不知道如何获得答案,似乎响应没有方法来获取它
@Component
public class DataUsageInterceptor extends HandlerInterceptorAdapter {
private static final Logger LOG = LoggerFactory.getLogger(CheckController.class);
@Override
public boolean preHandle(
HttpServletRequest request,
HttpServletResponse response,
Object handler
) throws Exception {
System.out.println("---- " + request.getContentLength() + "----------");
return super.preHandle(request, response, handler);
}
@Override
public void afterCompletion(
HttpServletRequest request,
HttpServletResponse response,
Object handler, Exception ex
) throws Exception {
System.out.println("---------- get length here ----------------");
}
}
答案 0 :(得分:0)
我认为您可以从响应标头中获取内容大小。它似乎是这样的:
response.getHeader("Content-Length");
供参考:
https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html(第14.13节内容长度)