You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Laurie Harper <la...@holoweb.net> on 2008/04/22 19:23:05 UTC

Re: Two problems: Struts2 + https + file load and Struts2 + file load + java.lang.IllegalStateException: getOutputStream()

Filippov, Andrey wrote:
> Hello everybody!
> 
> I am trying to load file from DB. I use https. In Mozilla I get only one exception but everything finally works. Here is my code and stack trace:
> 
> public String execute() throws Exception {
>             super.execute();
> 
>             byte[] file = null;
>             PolicyFileVO policyFile = polInfoInstance.getPolicyFileById(fileId);
>             file = policyFile.getFile();
>             String fileType = polInfoInstance.getFileTypeById(policyFile.getType())
>                         .getContentType();
>             if (file != null) {
>                   this.response.setCharacterEncoding("utf-8");
> 
> 
>                   String fileName = policyFile.getFile_name();
> 
> 
>                   if (!fileType.startsWith("image")) {
>                         this.response.addHeader("Content-Disposition",
>                                    "attachment; filename=" + fileName);
>                   }
> //                final ServletContext sc = ServletActionContext.getServletContext();
>                   this.response.setContentType(fileType);
>                   this.response.setContentLength(file.length);
>                   OutputStream o = null;
>                   try {
>                         o = response.getOutputStream();
>                         o.write(file);
>                         o.flush();
>                   } catch (java.lang.IllegalStateException ex) {
>                         log.error("IllegalStateException in FileContent.execute() method " + ex);
>                   }catch (java.io.IOException ex) {
>                         log.error("IOException in FileContent.execute() method " + ex);
>                   }catch (java.lang.Exception ex) {
>                         log.error("Exception in FileContent.execute() method " + ex);
>                   }finally{
>                         if (o != null){
>                              response.flushBuffer();
>                              o.close();
>                         }
>                   }
>             }

There's no return statement in there, which shouldn't even compile...

> 16:52:20,832 ERROR [UIBean] error when rendering
> java.lang.IllegalStateException: getOutputStream() has already been called for this response
>       at org.apache.catalina.connector.Response.getWriter(Response.java:604)
> ............................................................................................
> 16:52:21,004 ERROR [[default]] Servlet.service() for servlet default threw exception
> java.io.IOException: Error including path '/layouts/four_rows_layout.jsp'. java.lang.IllegalStateException: getOutputStream() has already been called for this response
>       at org.apache.tiles.servlet.context.ServletTilesRequestContext.include(ServletTilesRequestContext.java:214)
>       at org.apache.tiles.servlet.context.ServletTilesRequestContext.dispatch(ServletTilesRequestContext.java:183)
>       at org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:417)
>       at org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:368)

This suggests Struts is trying to forward to a JSP after your action 
completes, which won't work since you've already sent a file back to the 
browser. You execute() method should be returning 'null' to tell Struts 
not to do this.

> And the second part of my problem happens only in IE6 - when dialog of opening/saving file instead of normal file name like myTest.pdf reflect the action name - something like this: FileContent_action?fileId=5046 (only when I use https - in http it looks fine - myTest.pdf).

The file name to save to is specified by the Content-Disposition header, 
which you are not setting consistently. Sending a correct header every 
time should fix this problem.

L.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


RE: Two problems: Struts2 + https + file load and Struts2 + file load + java.lang.IllegalStateException: getOutputStream()

Posted by UncleLongHair <un...@tidd.cc>.
I was getting the "getOutputStream() has already been called for this
response" problem and figured out a solution.  In my case, I was able to
switch from getOutputStream() to getWriter() because I was dealing with text
files, and the error message stopped.

I think the error message is misleading, I think you can get it if either
getOutputStream() gets called more than once OR if both getOutputStream()
and getWriter() are called.  I wonder if the idea is to prevent both text
and binary data from being written into a response.

I understand that this might not help everyone, but I thought I'd offer it
up.  It might be worthwhile to look at the source for HttpServletResponse to
see the conditions under which the error is thrown.

Uncle



Brad A Cupit wrote:
> 
>>> And it is strange because it happens only when I use https and
>>> IE6. When it is http and IE6 it seems to be fine.
> 
> these links may help:
> http://eirikhoem.wordpress.com/2007/06/15/generated-pdfs-over-https-with
> -internet-explorer/
> 
> http://forum.java.sun.com/thread.jspa?threadID=233446&forumID=45
> 
> Brad Cupit
> Louisiana State University - UIS
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/Two-problems%3A-Struts2-%2B-https-%2B-file-load-and-Struts2-%2B-file-load-%2B-java.lang.IllegalStateException%3A-getOutputStream%28%29-tp16832576p17809331.html
Sent from the Struts - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


RE: Two problems: Struts2 + https + file load and Struts2 + file load + java.lang.IllegalStateException: getOutputStream()

Posted by Brad A Cupit <br...@lsu.edu>.
>> And it is strange because it happens only when I use https and
>> IE6. When it is http and IE6 it seems to be fine.

these links may help:
http://eirikhoem.wordpress.com/2007/06/15/generated-pdfs-over-https-with
-internet-explorer/

http://forum.java.sun.com/thread.jspa?threadID=233446&forumID=45

Brad Cupit
Louisiana State University - UIS

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


RE: Two problems: Struts2 + https + file load and Struts2 + file load + java.lang.IllegalStateException: getOutputStream()

Posted by "Filippov, Andrey" <an...@t-systems.ru>.
Thanx a lot. After I insert "return null" the exception about

> 16:52:20,832 ERROR [UIBean] error when rendering
> java.lang.IllegalStateException: getOutputStream() has already been called > for this response

stopped. But I've still got the problem with IE6 - instead of file name I have the name of my action - as I understand IE6 takes the last part of my url because it looks like filename (dot) extension (FileContent.action?fileId=5067). And it is strange because it happens only when I use https and IE6. When it is http and IE6 it seems to be fine.  Could anyone help with this stuff?

Thank you very much!

Sincerely yours.

-----Original Message-----
From: news [mailto:news@ger.gmane.org] On Behalf Of Laurie Harper
Sent: Tuesday, April 22, 2008 9:23 PM
To: user@struts.apache.org
Subject: Re: Two problems: Struts2 + https + file load and Struts2 + file load + java.lang.IllegalStateException: getOutputStream()

Filippov, Andrey wrote:
> Hello everybody!
>
> I am trying to load file from DB. I use https. In Mozilla I get only one exception but everything finally works. Here is my code and stack trace:
>
> public String execute() throws Exception {
>             super.execute();
>
>             byte[] file = null;
>             PolicyFileVO policyFile = polInfoInstance.getPolicyFileById(fileId);
>             file = policyFile.getFile();
>             String fileType = polInfoInstance.getFileTypeById(policyFile.getType())
>                         .getContentType();
>             if (file != null) {
>                   this.response.setCharacterEncoding("utf-8");
>
>
>                   String fileName = policyFile.getFile_name();
>
>
>                   if (!fileType.startsWith("image")) {
>                         this.response.addHeader("Content-Disposition",
>                                    "attachment; filename=" + fileName);
>                   }
> //                final ServletContext sc = ServletActionContext.getServletContext();
>                   this.response.setContentType(fileType);
>                   this.response.setContentLength(file.length);
>                   OutputStream o = null;
>                   try {
>                         o = response.getOutputStream();
>                         o.write(file);
>                         o.flush();
>                   } catch (java.lang.IllegalStateException ex) {
>                         log.error("IllegalStateException in FileContent.execute() method " + ex);
>                   }catch (java.io.IOException ex) {
>                         log.error("IOException in FileContent.execute() method " + ex);
>                   }catch (java.lang.Exception ex) {
>                         log.error("Exception in FileContent.execute() method " + ex);
>                   }finally{
>                         if (o != null){
>                              response.flushBuffer();
>                              o.close();
>                         }
>                   }
>             }

There's no return statement in there, which shouldn't even compile...

> 16:52:20,832 ERROR [UIBean] error when rendering
> java.lang.IllegalStateException: getOutputStream() has already been called for this response
>       at org.apache.catalina.connector.Response.getWriter(Response.java:604)
> ............................................................................................
> 16:52:21,004 ERROR [[default]] Servlet.service() for servlet default threw exception
> java.io.IOException: Error including path '/layouts/four_rows_layout.jsp'. java.lang.IllegalStateException: getOutputStream() has already been called for this response
>       at org.apache.tiles.servlet.context.ServletTilesRequestContext.include(ServletTilesRequestContext.java:214)
>       at org.apache.tiles.servlet.context.ServletTilesRequestContext.dispatch(ServletTilesRequestContext.java:183)
>       at org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:417)
>       at org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:368)

This suggests Struts is trying to forward to a JSP after your action
completes, which won't work since you've already sent a file back to the
browser. You execute() method should be returning 'null' to tell Struts
not to do this.

> And the second part of my problem happens only in IE6 - when dialog of opening/saving file instead of normal file name like myTest.pdf reflect the action name - something like this: FileContent_action?fileId=5046 (only when I use https - in http it looks fine - myTest.pdf).

The file name to save to is specified by the Content-Disposition header,
which you are not setting consistently. Sending a correct header every
time should fix this problem.

L.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org