确定 HTTP 响应的大小? [英] Determine size of HTTP Response?

查看:30
本文介绍了确定 HTTP 响应的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法确定HTTPServletResponse 内容的大小?我读了这个 get-size-of-http-response-in-java 问题,但遗憾的是我工作的地方我无法访问 CommonsIO :(

Is there a way to determine the size of the HTTPServletResponse content? I read this get-size-of-http-response-in-java question but sadly where I work I do not have access to CommonsIO :(

响应内容由单个复杂对象组成,因此我考虑将其写入临时文件,然后检查该文件.这不是我想在应用程序在生产中运行时进行诊断的事情,但我希望尽可能避免它.

The response content consists of a single complex object so I have considered writing it out to a temp file and then checking that file. This is not something I want to be doing as a diagnostic while the application is running in production though so want to avoid it if at all possible.

PS 我读了 erickson 的回答,但它提到了输入流,我想知道写出的对象的大小...如果 writeObject() 方法返回一个表示字节的数字,那就太好了写而不是 void...

PS I read erickson's answer but it mentioned input streams I want to know the size of the object being written out... Would be really nice if the writeObject() method returned a number representing bytes written instead of void...

推荐答案

我最终找到了一种方法来获得我想要的东西:

I eventually found a way to get what I wanted:

URLConnection con = servletURL.openConnection();
BufferedInputStream bif = new BufferedInputStream(con.getInputStream());
ObjectInputStream input = new ObjectInputStream(bif);
int avail = bif.available();

System.out.println("Response content size = " + avail);

这让我可以看到客户端的响应大小.我仍然想知道它在发送之前在服务器端是什么,但这是下一个最好的事情.

This allowed me to see the response size on the client. I still would like to know what it is on the server side before it is sent but this was the next best thing.

这篇关于确定 HTTP 响应的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆