Hi guys,
i'm doing a script that's building a CSV file then sending it to the browser so the user can download it.
Everything works fine, I get to download successfully the file, the output is perfect.
Except that I always receive this error...:
java.lang.IllegalStateException: getOutputStream() has already been called for this response
org.apache.coyote.tomcat5.CoyoteResponse.getWriter(CoyoteResponse.java:599)
org.apache.coyote.tomcat5.CoyoteResponseFacade.getWriter(CoyoteResponseFacade.java:163)
org.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java:122)
org.apache.jasper.runtime.JspWriterImpl.flushBuffer(JspWriterImpl.java:115)
org.apache.jasper.runtime.PageContextImpl.release(PageContextImpl.java:190)
org.apache.jasper.runtime.JspFactoryImpl.internalReleasePageContext(JspFactoryImpl.java:115)
org.apache.jasper.runtime.JspFactoryImpl.releasePageContext(JspFactoryImpl.java:75)
org.apache.jsp.WEB_002dINF.jsp.reports.multipleExportReport_jsp._jspService(multipleExportReport_jsp.java:94)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
org.apache.struts.action.RequestProcessor.doForward(RequestProcessor.java:1069)
org.apache.struts.tiles.TilesRequestProcessor.doForward(TilesRequestProcessor.java:274)
org.apache.struts.action.RequestProcessor.processForwardConfig(RequestProcessor.java:455)
org.apache.struts.tiles.TilesRequestProcessor.processForwardConfig(TilesRequestProcessor.java:320)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:279)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
com.mx.releasemgr.filters.ServletRequestFilter.doFilter(ServletRequestFilter.java:30)
com.mx.releasemgr.filters.RequestCharacterEncodingFilter.doFilter(RequestCharacterEncodingFilter.java:36)
com.mx.releasemgr.security.AbstractSecurityFilter.doFilter(AbstractSecurityFilter.java:54)
com.mx.releasemgr.db.hibernate.HibernateSessionFilter.doFilter(HibernateSessionFilter.java:67)
org.displaytag.filter.ResponseOverrideFilter.doFilter(ResponseOverrideFilter.java:125)
org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:75)
It really bugs me....I mean, it works fine, but that error always appear in my stack trace. Adding a try catch all exceptions around the method building the CSV file, it simply crash, I still get the error in the stack trace, but it failed sending the file to my browser...
Here's 2 function of my class:
public final ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception {
MultipleExportEditorForm myForm = (MultipleExportEditorForm) form;
if (myForm != null){
if (myForm.getFormFile() != null){
if (!myForm.getFormFile().getFileName().equals("")){
generalReport(response, myForm.getFormFile());
}
}
}
return mapping.findForward("edit/multipleexport");
}
public final void generalReport(HttpServletResponse response, FormFile file) throws IOException{
OutputStream out = response.getOutputStream();
response.setContentType("application/force-download");
response.setHeader("Content-Disposition","attachment; filename=multiple-export.csv");
try {
out.write(getInfos(file));
} catch (BadDelimiterException e) {
e.printStackTrace();
} catch (HibernateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
out.flush();
out.close();
}
I won't put the getInfos() method since it works and it not necessary here I think.
I would like to know what I can do to avoid having this error. Is there a way to insert thing into the output stream without getting it ? Because another framework (Struts I guess) seems to be calling it already. Any idea ?