• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Setting header and fetching OutputStream from Action

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, i really need some help here.
I want to have the user download a file while the button gets clicked, and the file is generated in runtime.
I wrote a simple Action class to test this. Here's what's inside the perform() method :
String strngContent = "asdfasdfasdfasdf";
// this header is required for the download dialog from the browser to pop up
response.setHeader("Content-Disposition", "attachment; filename=test.download");
response.getOutputStream().write(strngContent.getBytes());
return new ActionForward(mapping.getInput());
And this is the error message that i got :
org.apache.jasper.JasperException: getOutputStream() has already been called for this response
I wonder if there is any work around for this problem in Struts ? I mean, i've been looking in the Action and RequestProcessor source, and i couldnt find any clue.
Please help, thanks in advance.
 
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Albert,
It works for me with Struts 1.1 and Tomcat 4.1. What version of Struts are you using. It looks like 1.0.
Try 1.1.
And remember to call flush() on your Outputstream.

-Yoo-Jin
[ October 15, 2003: Message edited by: Yoo-Jin Lee ]
[ October 15, 2003: Message edited by: Yoo-Jin Lee ]
 
Yoo-Jin Lee
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just looked at getOutputStream and getWriter they both throw an java.lang.IllegalStateException.
---
java.lang.IllegalStateException - if the getOutputStream method has already been called for this response object
---
If you are using Struts 1.0 then there could be a bug or else both getOutputStream() and getWriter() are being called somewhere.
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The immediate problem you're running into is that you cannot get both an OutputStream and an Writer for the same response.
The underlying problem is that you are trying to generate a response in two different places. This does not look healthy. Either your Action generates the response, and you do not return an ActionForward but simply return null to indicate that you have already generated the response. Or the view generates the response -- the usual approach -- in which case the Action has no business retrieving an OutputStream from the response object.
You seem to be doing both at the same time, this is asking for trouble.
- Peter
 
Albert Gan
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello again.
Peter, i didnt intend to generate html output from my Action class. I just want to send a file so that the client could have a popup download dialog, and save the file.
Lee, thanks alot. guess what's wrong with my code ! i just miss the .flush() and i get the error message that the outputstream object has already been called twice. it's just a misleading error message, since in my code, i never call getOutputStream or getWriter twice .. or both.
Oh yeah, for those who need the popup download dialog to show up, you should set the Content-Length header with the size of the file and the Content-Type header to 'application/octet-stream', and the Content-Disposition to 'attachment; filename=<your filename>'
Thank you thank you thank you ....

[ October 15, 2003: Message edited by: Albert Gan ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic