java 响应输出流内容长度?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1756418/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Response outputstream content length?
提问by Trick
I am writing to output stream through various methods. How can I, before I close it, find out content length of the outputstream?
我正在通过各种方法写入输出流。我怎样才能在关闭它之前找出输出流的内容长度?
回答by Jon Skeet
The easiest way is probably to wrap it in another OutputStreamimplementation 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.
最简单的方法可能是将它包装在另一个OutputStream实现中,该实现转发所有写请求,但保留一个内部计数器。然后你只需写信。实施起来应该不会太难——事实上可能已经有了。
EDIT: Just guessing at a sensible name (CountingOutputStream) came up with an implementation in Apache Commons IO.
编辑:只是猜测一个合理的名称 ( CountingOutputStream) 想出了一个在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 itcould work out the content length), you may have problems due to needing to write the length beforewriting 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.
编辑:如其他地方所述,如果这是用于 HTTP 并且您的客户端尚未对完整数据进行缓冲(在这种情况下,我认为它可以计算出内容长度),您可能会因为需要而遇到问题在写入数据之前写入长度。在某些情况下,您可能会发现它会工作到特定大小(客户端缓冲)然后失败。在这种情况下,大卫的解决方案将是合适的。
回答by David
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:
问题是在开始将任何数据写入输出流之前,您必须在响应标头中设置内容长度。所以你的选择是:
- 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.
- 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.
- 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.
- Resign yourself to the fact that you won't be able to report the content length in the response header.
- 使用 ByteOutputStream 将数据写入 byte[] 数组,然后在获得数据大小后将其复制到响应输出流。但是,如果您正在编写大文件,这显然不是一种选择。
- 将数据写入临时文件,然后在获得文件大小后将其复制到响应输出。根据您在做什么,这可能会导致不可接受的性能损失。
- 根据首先生成数据的成本,您可以生成一次,然后将其丢弃以获取计数,然后再次生成。猜测这不太可能是一个现实的解决方案。
- 接受您将无法在响应标头中报告内容长度的事实。
回答by Dmitry
You may consider writing to your own ByteArrayOutputStream and flush it to the response output stream at the very end.
您可以考虑写入您自己的 ByteArrayOutputStream 并在最后将其刷新到响应输出流。

