我需要计算写入 HttpServletResponse 的实际字节数,以确保整个文件已经传输。我的天真做法如下:
InputStream instream = InputStream instream = new FileInputStream("myfile.zip");
ServletOutputStream outputStream = res.getOutputStream();
CountingInputStream countingInputStream = new CountingInputStream(instream);
IOUtils.copy(countingInputStream, outputStream);
log.debug("Expected count: " + String.valueOf(contentLength));
log.debug("getCount: " + countingInputStream.getCount());
outputStream.close();
countingInputStream.close();
instream.close();
在Jetty上,这种方法运作良好,但是在Tomcat上,即使客户端取消了下载,Tomcat仍然返回完整的输出流以供编写。
此外,这种方法在Tomcat 6上多年来也一直表现良好。我们现在必须迁移到Tomcat 7并遇到了这个问题。
有没有一种方法可以计算实际发送到客户端的字节数?
byte[] bytes = IOUtils.toByteArray(inputStream);
- Suzon