title: Spring-MVC-Java based Configuration: getOutputStream() has already been called for this response
body: <p>I am using Spring 3 with Java based configuration.</p>
  
  <p>I have a module that exports an Excel sheet to the user (i.e. download a sheet to the user)</p>
  
  <p>I got the following exception at log .... but it exports fine:</p>
  
  <blockquote>
    <p>java.lang.IllegalStateException: getOutputStream() has already been
    called for this response
                    at org.apache.catalina.connector.Response.getWriter(Response.java:639)
                    at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:214)
                    at javax.servlet.ServletResponseWrapper.getWriter(ServletResponseWrapper.java:105)
                    at javax.servlet.ServletResponseWrapper.getWriter(ServletResponseWrapper.java:105)
                    at org.springframework.web.servlet.view.freemarker.FreeMarkerView.processTemplate(FreeMarkerView.java:366)
                    at org.springframework.web.servlet.view.freemarker.FreeMarkerView.doRender(FreeMarkerView.java:283)
                    at org.springframework.web.servlet.view.freemarker.FreeMarkerView.renderMergedTemplateModel(FreeMarkerView.java:233)
                    at org.springframework.web.servlet.view.AbstractTemplateView.renderMergedOutputModel(AbstractTemplateView.java:167)
                    at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:262)
                    at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1157)
                    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:927)
                    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:827)
                    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
                    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
                    at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
                    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
                    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
                    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
                    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:311)
                    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:116)
                    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:83)
                    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
                    at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113)
                    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
                    at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:101)
                    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)</p>
  </blockquote>
  
  <p>After investigation,I found out that the getOutputStream() method can only be called on a response once.
  However I call it every time the user needs to export the sheet as the following:</p>
  
  <pre><code>public void exportExcel(HttpServletResponse response) throws Exception {
  
      response.setContentType("application/vnd.ms-excel");
  
      response.setHeader("Content-disposition", "attachment;filename=" + sheetName + "");
  
  
      ByteArrayOutputStream excelFileByteArrayOutputStream =
  
      excelFileByteArrayOutputStream.writeTo(response.getOutputStream());
      excelFileByteArrayOutputStream.flush();
      response.flushBuffer();
      excelFileByteArrayOutputStream.close();
    }
  </code></pre>
  
  <p>Freemarker configuration:</p>
  
  <pre><code>@Bean
    public FreeMarkerViewResolver viewResolver() {
      FreeMarkerViewResolver freeMarkerViewResolver = new FreeMarkerViewResolver();
      freeMarkerViewResolver.setCache(true);
      freeMarkerViewResolver.setPrefix("");
      freeMarkerViewResolver.setSuffix(".ftl");
      return freeMarkerViewResolver;
    }
  
    @Bean
    public FreeMarkerConfigurer freemarkerConfig() {
      FreeMarkerConfigurer freeMarkerConfigurer = new FreeMarkerConfigurer();
      freeMarkerConfigurer.setTemplateLoaderPath("/WEB-INF/freemarker/");
      Properties settings = new Properties();
      settings.setProperty("number_format", "0.######");
      freeMarkerConfigurer.setFreemarkerSettings(settings);
      Map variables = new java.util.HashMap&lt;String, Object&gt;();
  
      variables.put("xml_escape", new XmlEscape());
      freeMarkerConfigurer.setFreemarkerVariables(variables);
      return freeMarkerConfigurer;
    }
  </code></pre>
  
  <p>@Controlle ... after the sheet is exported,it never redirects to the home page as I specified:</p>
  
  <pre><code>  @RequestMapping(value = "/smth", method = RequestMethod.GET)
    public ModelAndView exportSheet(HttpServletResponse response,
                                                             Bean aBean,
                                                             BindingResult result) throws Exception {
  
  //Some Code
      return new ModelAndView("/home");
    }
  </code></pre>
  
  <p>My WebApplicationInitializer:</p>
  
  <pre><code>public class SpringWebApplicationInitializer implements WebApplicationInitializer {
  
    @Override
    public void onStartup(final ServletContext servletContext) throws ServletException {
  
      // Create the 'root' Spring application context
      AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
      rootContext.register(WebConfiguration.class);
  
      // Manage the lifecycle of the root application context
      servletContext.addListener(new ContextLoaderListener(rootContext));
  
      // Create the dispatcher servlet's Spring application context
      AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext();
      dispatcherContext.register(DispatcherConfiguration.class);
  
      // Register and map the dispatcher servlet
      ServletRegistration.Dynamic dispatcher =
        servletContext.addServlet("dispatcher", new DispatcherServlet(dispatcherContext));
      dispatcher.setLoadOnStartup(1);
      dispatcher.addMapping("/");
  
      // Register Spring Security Filter
      DelegatingFilterProxy securityFilter = new DelegatingFilterProxy();
      FilterRegistration.Dynamic securityFilterRegistration =
        servletContext.addFilter("springSecurityFilterChain", DelegatingFilterProxy.class);
      securityFilterRegistration.addMappingForUrlPatterns(null, false, "/*");
    }
  
  
  }
  </code></pre>
  
  <p>Any ideas how to tickle this issue!</p>