确定 HTTP 响应的大小?

发布于 2024-10-26 13:19:55 字数 437 浏览 3 评论 0原文

有没有办法确定 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

洛阳烟雨空心柳 2024-11-02 13:19:55

如果您有权访问响应标头,则可以读取 Content-Length

以下是响应标头的示例:

(Status-Line):HTTP/1.1 200 OK
Connection:Keep-Alive
Date:Fri, 25 Mar 2011 16:26:56 GMT
Content-Length:728

请查看:标头字段定义

If you have access to the response header, you can read the Content-Length.

Here is a example of a response header:

(Status-Line):HTTP/1.1 200 OK
Connection:Keep-Alive
Date:Fri, 25 Mar 2011 16:26:56 GMT
Content-Length:728

Check this out: Header Field Definitions

我纯我任性 2024-11-02 13:19:55

似乎就是您正在寻找的为了:

DataOutputStream dos = new DataOutputStream(response.getOutputStream());
...
int len = dos.size();

This seems to be what you're looking for:

DataOutputStream dos = new DataOutputStream(response.getOutputStream());
...
int len = dos.size();
∞琼窗梦回ˉ 2024-11-02 13:19:55

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

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);

这使我能够看到客户端上的响应大小。我仍然想在发送之前知道服务器端的内容,但这是下一个最好的事情。

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.

客…行舟 2024-11-02 13:19:55

假设使用ObjectOutputStream,围绕java.io.ByteArrayOutputStream构建它:

ByteArrayOutputStream contentBytes = new ByteArrayOutputStream();

ObjectOutputStream objectOut = new ObjectOutputStream(contentBytes);

objectOut.writeObject(content);

int contentLength = contentBytes.size();

发送内容,

contentBytes.writeTo(connection.getOutputStream());

然后您可以使用connection 无论您是什么正在从中获取您的 OutputStream

迟到总比不到好,对吧?

Assuming the use of ObjectOutputStream, build it around a java.io.ByteArrayOutputStream:

ByteArrayOutputStream contentBytes = new ByteArrayOutputStream();

ObjectOutputStream objectOut = new ObjectOutputStream(contentBytes);

objectOut.writeObject(content);

int contentLength = contentBytes.size();

And then you can send the content with

contentBytes.writeTo(connection.getOutputStream());

where connection is whatever you're getting your OutputStream from.

Better late than never, right?

惟欲睡 2024-11-02 13:19:55

如果你使用 Catalina,这对我有用:

import org.apache.catalina.connector.ResponseFacade
...
long len=((ResponseFacade) response).getContentWritten();

if you use Catalina, this worked for me:

import org.apache.catalina.connector.ResponseFacade
...
long len=((ResponseFacade) response).getContentWritten();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文