Hello All,
I am experiencing a very strange problem and don't know exactly how to fix it. The
problem is garbled junk characters are being prepended to the response output stream
that the browser receives. This only happens for Internet Explorer and it seems very
random. It is very difficult to reproduce. I've never seen it happen under Mozilla.
Before I launch into my questions, let me explain our setup.
We have Tomcat 4.1.12 behind Apache 1.3.27. Running on tomcat is a custom
application. Part of what it does is maintain a library of digital assets. These
assets can be movies, pdfs, images, etc. When browsing the library of assets, you
have several options: view thumbnail, view fullsize, download, etc. While all static
images used in the web pages are served up by Apache, all of these assets are served
by a special 'proxy' servlet in tomcat. This is done primarily for security. All
access to this restricted material is monitored via this proxy servlet.
The code for the proxy is basically:
File file = new File(filePath);
FileInputStream fis = new FileInputStream(file);
ServletOutputStream outStream = response.getOutputStream();
long fileLength = file.length();
response.setContentLength((int) fileLength);
response.addHeader("Pragma", "no-cache");
if(action.equals("download")) {
// We need to set the attachment header that only some browsers listen to. (the
good ones)
response.setHeader("Content-Disposition","attachment; filename=" + fileName + ";");
// Then because some browsers don't follow standards (IE) we have to set the mime
type as a bogus value
response.setContentType("doner/x-fileshare-native");
} else {
// if we are just viewing, let the browser know the correct mime type.
response.setContentType(asset.getMimeType());
}
byte[] buffer = new byte[1024];
int numBytes = 0;
int totalBytes = 0;
while(totalBytes < fileLength) {
numBytes = fis.read(buffer);
totalBytes += numBytes;
outStream.write(buffer);
}
fis.read(buffer);
fis.close();
The reason I post this code is because I believe that the garbled junk is the remains
of an aborted file download. My guess is that sometime during the while loop that
writes the contents of the file to the stream, the user opened another page (possibly
without the browser closing the connection?). This problem is reproducible (though
not very easily) when a large file is opened for viewing or download. While the
transfer is occuring, click on another link. 1 out of 3 times you see the garbled
junk. Sometimes it happens more often, sometimes less.
So, am I going off in the wrong direction with this? If not, is there anyway to check
to see if the connection was closed or aborted during servlet processing? Does anyone
have any ideas or thoughts about this?
Thanks for any help!
-Mike