This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Rust Web Development and have Bastian Gruber on-line!
See this thread for details.
Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Saving word document in browser doesn't work

 
Bartender
Posts: 1840
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 510
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does it work in another browser like Internet Explorer?
 
Mike London
Bartender
Posts: 1840
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 67618
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please read this article.
[ July 27, 2007: Message edited by: Bear Bibeault ]
 
Mike London
Bartender
Posts: 1840
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ben!

M
 
Michael Ku
Ranch Hand
Posts: 510
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 1840
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic