Our application gets a ByteArrayOutputStream in the JSP page -- a word document. We have the JSP header set to "application/msword".
However, when the file gets saved to the user's desktop via the "save as" dialog that pops up in Firefox, it always has 6 bytes more than the ByteArrayOutputStream equivalent (that is, the disk file has 6 more bytes).
Thus, when you try to open the disk file, it is just gibberish.
We've verified that we can write the ByteArrayOutputStream to the disk and successfully open it in Word. So, at least we do have all the file (and nothing extra) in what we're sending to the JSP page.
Perhaps there's another Http header we need to set?
Would using the trim-directive-whitspace fix the JSP problem? The following is from a Sun Java EE 5 tutorial.
White space included in the template text of JSP pages is preserved by default. This can have undesirable effects. For example, a carriage return added after a taglib directive would be added to the response output as an extra line.
If you want to eliminate the extra white space from the page, you can add a trim-directive-whitespaces element to a jsp-property-group element in the deployment descriptor and set it to true.
I've seen someone succeed in streaming binary data from a JSP. They made sure the very first two characters were '<%' and then called 'return' right after flushing the output so that nothing under it would be interpreted at run time. He claimed that it worked in that version of Tomcat. I wouldn't bet on it working anywhere else or even in a new version of the same server.
For me, it would be a lot simpler to do this from a servlet. Is there any particular reason that you want to do this from a JSP?
Joined: Jul 12, 2002
The way we got around it was to do a session.refresh(). Then we were able to save the file successfully from the JSP.
We do need to move this code to an Action class, but for the moment, this fixed the problem.
Thanks for all the great replies!
subject: Saving word document in browser doesn't work