Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Saving word document in browser doesn't work

 
Mike London
Ranch Hand
Posts: 1192
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

Any ideas would be appreciated.

Thanks.

Thanks.

-- M
 
Michael Ku
Ranch Hand
Posts: 510
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does it work in another browser like Internet Explorer?
 
Mike London
Ranch Hand
Posts: 1192
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, we're currently having problems with IE too, but not in this code portion of the code.

Needless to say, we need to fix the IE problem with our application since it since it prevents us from checking how things work in "another browser".

Thanks.

M
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a common problem.
JSP is not a good technology for sending binary data to browsers.
JSP compilers often add whitespace to the beginning and/or end of the generated servlet's output.

Do this from a servlet and you'll have much more control over the output.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64837
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please read this article.
[ July 27, 2007: Message edited by: Bear Bibeault ]
 
Mike London
Ranch Hand
Posts: 1192
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ben!

M
 
Michael Ku
Ranch Hand
Posts: 510
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Probably not.

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?
 
Mike London
Ranch Hand
Posts: 1192
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!

--M
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic