确定 HTTP 响应的大小?
有没有办法确定 HTTPServletResponse
内容的大小?我读了这个 get-size-of-http-response-in-java 问题,但遗憾的是我工作的地方无法访问 CommonsIO :(
响应内容由单个复杂对象组成,因此我考虑将其写入临时文件,然后检查该文件。这不是我想要的在应用程序在生产中运行时作为诊断,因此希望尽可能避免它。
PS 我读了埃里克森的答案,但它提到了输入流,我想知道正在写出的对象的大小......会是。如果 writeObject()
方法返回一个表示写入字节的数字而不是 void
,那就太好了...
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 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
...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您有权访问响应标头,则可以读取
Content-Length
。以下是响应标头的示例:
请查看:标头字段定义
If you have access to the response header, you can read the
Content-Length
.Here is a example of a response header:
Check this out: Header Field Definitions
这似乎就是您正在寻找的为了:
This seems to be what you're looking for:
我最终找到了一种获得我想要的东西的方法:
这使我能够看到客户端上的响应大小。我仍然想在发送之前知道服务器端的内容,但这是下一个最好的事情。
I eventually found a way to get what I wanted:
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.
假设使用
ObjectOutputStream
,围绕java.io.ByteArrayOutputStream
构建它:发送内容,
然后您可以使用
connection
无论您是什么正在从中获取您的OutputStream
。迟到总比不到好,对吧?
Assuming the use of
ObjectOutputStream
, build it around ajava.io.ByteArrayOutputStream
:And then you can send the content with
where
connection
is whatever you're getting yourOutputStream
from.Better late than never, right?
如果你使用 Catalina,这对我有用:
if you use Catalina, this worked for me: