响应输出流内容长度?

发布于 2024-08-11 12:12:34 字数 42 浏览 6 评论 0原文

我正在通过各种方法写入输出流。在关闭它之前,如何找出输出流的内容长度?

I am writing to output stream through various methods. How can I, before I close it, find out content length of the outputstream?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

—━☆沉默づ 2024-08-18 12:12:34

最简单的方法可能是将其包装在另一个 OutputStream 实现中,该实现转发所有写入请求,但保留一个内部计数器。然后你只需写信即可。实施起来应该不会太难——事实上可能已经有一个了。

编辑:只是猜测一个合理的名称(CountingOutputStream)在 Apache Commons 中提出了一个实现IO。

编辑:如其他地方所述,如果这是针对 HTTP 并且您的客户端尚未缓冲完整数据(在这种情况下,我认为它可以计算出内容长度),那么您由于需要在写入数据之前写入长度,可能会出现问题。在某些情况下,您可能会发现它会工作到一定大小(客户端缓冲),然后失败。在这种情况下,大卫的解决方案将是合适的。

The easiest way is probably to wrap it in another OutputStream implementation which forwards on all the write requests, but keeps an internal counter. Then you just write to that instead. Shouldn't be too hard to implement - and indeed there may be one already.

EDIT: Just guessing at a sensible name (CountingOutputStream) came up with an implementation in Apache Commons IO.

EDIT: As noted elsewhere, if this is for HTTP and your client isn't already doing buffering of the full data (in which case I'd have thought it could work out the content length), you may have problems due to needing to write the length before writing the data. In some cases you may find that it will work up to a certain size (which the client buffers) and then fail. In that case, David's solutions will be appropriate.

殤城〤 2024-08-18 12:12:34

问题是您必须在开始将任何数据写入输出流之前在响应标头中设置内容长度。因此,您的选择是:

  1. 使用 ByteOutputStream 将数据写入 byte[] 数组,然后在获得数据大小后将其复制到响应输出流。但是,如果您要写入大文件,这显然不是一个选择。
  2. 将数据写入临时文件,然后在获得文件大小后将其复制到响应输出。根据您正在执行的操作,这可能会造成不可接受的性能损失。
  3. 根据首先生成数据的成本有多高,您可以生成一次,然后将其丢弃以获取计数,然后再次生成。猜测这不太可能是一个现实的解决方案。
  4. 接受这样一个事实:您将无法在响应标头中报告内容长度。

The problem is that you must set the content length in the response header before you start writing any data to the output stream. So your options are:

  1. Write the data to a byte[] array using ByteOutputStream and then copy that to the response output stream once you have the size of the data. However, if you're writing large files, this obviously isn't an option.
  2. Write the data to a temp file and then copy that to the response output once you get the file size. Depending on what you're doing, this may have a performance penalty that is unacceptable.
  3. Depending on how expensive it is to generate the data in the first place, you could generate it once, and throw it away to get the count and then generate it again. Guessing that this is unlikely to be a realistic solution.
  4. Resign yourself to the fact that you won't be able to report the content length in the response header.
温柔一刀 2024-08-18 12:12:34

您可以考虑写入自己的 ByteArrayOutputStream 并在最后将其刷新到响应输出流。

You may consider writing to your own ByteArrayOutputStream and flush it to the response output stream at the very end.

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