2

我正在使用 Spring 3 和基于 Java 的配置。

我有一个向用户导出 Excel 工作表的模块(即向用户下载工作表)

我在日志中遇到以下异常 .... 但它可以正常导出:

java.lang.IllegalStateException: getOutputStream() 已经在 org.apache.catalina.connector.Response.getWriter(Response.java:639) 在 org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade. java:214) 在 javax.servlet.ServletResponseWrapper.getWriter(ServletResponseWrapper.java:105) 在 javax.servlet.ServletResponseWrapper.getWriter(ServletResponseWrapper.java:105) 在 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) 在 org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:262) 在 org.springframework.web 的 org.springframework.web.servlet.view.AbstractTemplateView.renderMergedOutputModel(AbstractTemplateView.java:167) .servlet.DispatcherServlet.render(DispatcherServlet.java:1157) 在 org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:927) 在 org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:827) ) 在 org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882) 在 org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778) 在 javax.servlet.http.HttpServlet.service( HttpServlet.java:621) 在 javax.servlet.http.HttpServlet.service(HttpServlet.java:728) 在 org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305) 在 org.apache.catalina.core.ApplicationFilterChain.doFilter (ApplicationFilterChain.java:210) 在 org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:311) 在 org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:116 ) 在 org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323) 在 org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323) 在 org.springframework.security 的 org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113) .web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:101) 在 org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)doFilter(SessionManagementFilter.java:101) at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)doFilter(SessionManagementFilter.java:101) at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)

经过调查,我发现 getOutputStream() 方法只能在响应上调用一次。但是,每次用户需要按以下方式导出工作表时,我都会调用它:

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();
  }

Freemarker配置:

@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<String, Object>();

    variables.put("xml_escape", new XmlEscape());
    freeMarkerConfigurer.setFreemarkerVariables(variables);
    return freeMarkerConfigurer;
  }

@Controlle ...导出工作表后,它永远不会按照我的指定重定向到主页:

  @RequestMapping(value = "/smth", method = RequestMethod.GET)
  public ModelAndView exportSheet(HttpServletResponse response,
                                                           Bean aBean,
                                                           BindingResult result) throws Exception {

//Some Code
    return new ModelAndView("/home");
  }

我的 WebApplicationInitializer:

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, "/*");
  }


}

任何想法如何解决这个问题!

4

0 回答 0