struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Laurie Harper <lau...@holoweb.net>
Subject Re: Two problems: Struts2 + https + file load and Struts2 + file load + java.lang.IllegalStateException: getOutputStream()
Date Tue, 22 Apr 2008 17:23:05 GMT
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


Mime
View raw message