I need to count the actual bytes written to the HttpServletResponse to make sure the whole file has been migrated. My naive approach is this:
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();
Although this approach works well on Jetty, Tomcat always returns the full output, even if the client canceled the download.
In addition, it also worked well for many years on Tomcat 6. Now we had to upgrade to Tomcat 7 and experience this problem.
Is there a way to count the bytes that were actually sent to the client?
Randy source
share