aspose file tools*
The moose likes JSP and the fly likes Saving word document in browser doesn't work Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Saving word document in browser doesn Watch "Saving word document in browser doesn New topic
Author

Saving word document in browser doesn't work

Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1034
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

Joined: Apr 20, 2002
Posts: 510
Does it work in another browser like Internet Explorer?
Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1034
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

Joined: Dec 11, 2004
Posts: 13410

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.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60077
    
  65

Please read this article.
[ July 27, 2007: Message edited by: Bear Bibeault ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1034
Thanks Ben!

M
Michael Ku
Ranch Hand

Joined: Apr 20, 2002
Posts: 510
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

Joined: Dec 11, 2004
Posts: 13410

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

Joined: Jul 12, 2002
Posts: 1034
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Saving word document in browser doesn't work
 
Similar Threads
Acrobat Plugin should first open, then I want to stream a PDF
problems in showing images in jsp with contentType= application /msword
repost with more detail on problem of download servlet in Internet Explore
Making a File load in a Web Sphere application?
Export data to Microsoft Word