确定HTTP响应的大小? [英] Determine size of HTTP Response?
问题描述
有没有办法确定 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屋!