Question

I have a java servlet that uses ServletOutputStream to stream binary files from the application server to a browser. The code that does this has been in prod for a rather long time, and works really well the vast majority of the time. My users are reporting an odd problem that only happens in firefox. After the ServletOutputStream finishes sending the file, firefox displays the file in the standard firefox window as jibberish strings of text, as opposed to prompting the user to select the application they would like to use. This doesn't consistently happen with any one file or file type. I have personally seen it happen once, but when I tried to download the file again, it worked correctly. A few users using firefox report the problem more frequently. Unfortunately no exceptions are being thrown to any logs, and I can't seem to reproduce it confidently at all.

The application is running on Java 6 and tomcat 6, and is fronted by apache. Any ideas on what could be going on, or how to troubleshoot this?

The code:

File target = file.getAttachment();
response.setContentType(file.getMimeType());
response.setContentLength(new Long(file.getFileSize()).intValue());
response.addHeader("Content-Disposition", "attachment; filename=" + file.getFileName() + ".foo;");
response.addHeader("Cache-Control", "max-age=0");
ServletOutputStream stream=response.getOutputStream();
BufferedInputStream fif=new BufferedInputStream(new FileInputStream(target));
int data;
int counter = 0;
try {
  while((data=fif.read())!=-1) {

    stream.write(data);
    //System.err.println("A   counter: " + counter);
    counter++;
    //if (counter > 249000) {
        //throw new Exception("Foo this bar!");
    //}
  }
  System.err.println("counter: " + counter);
} catch(SocketException x) {
  // catch socket reset by peer errors here, and ignore them.
  x.printStackTrace();
}
fif.close();
stream.close();

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top