用于 Servlet 3.0 的 MultipartResolver 的 Spring 4 Java 配置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23570014/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 00:02:41  来源:igfitidea点击:

Spring 4 Java Config for MultipartResolver for Servlet 3.0

javaspringspring-mvcservletsspring-java-config

提问by Jay Goettelmann

I'm taking an all-Java approach to Spring MVC configuration and cannot figure out how to associate a MultipartConfigElementwith my DispatcherServletprogrammatically.

我正在对 Spring MVC 配置采用全 Java 方法,但无法弄清楚如何以编程方式将 aMultipartConfigElement与我的关联DispatcherServlet

Spring documentation states:

Spring 文档指出:

In order to use Servlet 3.0 based multipart parsing, you need to mark the DispatcherServlet with a "multipart-config" section in web.xml, or with a javax.servlet.MultipartConfigElement in programmatic Servlet registration...

为了使用基于 Servlet 3.0 的多部分解析,您需要在 web.xml 中使用“multipart-config”部分或在编程 Servlet 注册中使用 javax.servlet.MultipartConfigElement标记 DispatcherServlet ...

http://docs.spring.io/spring/docs/4.0.4.RELEASE/spring-framework-reference/htmlsingle/#mvc-multipart

http://docs.spring.io/spring/docs/4.0.4.RELEASE/spring-framework-reference/htmlsingle/#mvc-multipart

Here is my WebApplicationInitializercode:

这是我的WebApplicationInitializer代码:

public class DispatcherServletInitializer implements WebApplicationInitializer {

    private static final Logger logger = LoggerFactory.getLogger(DispatcherServletInitializer.class);

    @Override
    public void onStartup(ServletContext container) {

        // Create the 'root' Spring application context
        AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
        rootContext.register(AppConfig.class);

        // Manage the lifecycle of the root application context
        container.addListener(new ContextLoaderListener(rootContext));

        // Create the dispatcher servlet's Spring application context
        AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext();
        dispatcherContext.register(WebConfig.class);

        //HOW CAN I ASSOCIATE THIS CONFIG WITH MY DISPATCHER SERVLET?
        MultipartConfigElement config = new MultipartConfigElement("C:\Temp", 20848820, 418018841, 1048576);
        DispatcherServlet dispatcherServlet = new DispatcherServlet(dispatcherContext);

        // Register and map the dispatcher servlet
        ServletRegistration.Dynamic dispatcher = 
            container.addServlet("dispatcher", dispatcherServlet);
        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("/*");
    }

}

How do I associate the MultipartConfigElementwith my DispatcherServlet? I don't see any method like setMultipartConfiguration or any constructor that accepts it.

我如何将MultipartConfigElement与我的DispatcherServlet? 我没有看到任何像 setMultipartConfiguration 这样的方法或任何接受它的构造函数。

Also note that my WebConfig declares a MultipartResolver:

另请注意,我的 WebConfig 声明了一个MultipartResolver

@Bean
public StandardServletMultipartResolver multipartResolver(){
    return new StandardServletMultipartResolver();
}

But the Spring documentation states:

但是 Spring 文档指出:

Configuration settings such as maximum sizes or storage locations need to be applied at that Servlet registration level as Servlet 3.0 does not allow for those settings to be done from the MultipartResolver.

Any guidance would be greatly appreciated.

任何指导将不胜感激。

采纳答案by Artem Bilan

Looks like you need this:

看起来你需要这个:

ServletRegistration.Dynamic dispatcher = 
            container.addServlet("dispatcher", dispatcherServlet);
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/*");

dispatcher.setMultipartConfig(new MultipartConfigElement("/tmp", 1024*1024*5, 1024*1024*5*5, 1024*1024));

回答by Jarekczek

Here is the solution compatible with AbstractAnnotationConfigDispatcherServletInitializerway of configuring the servlet. This is a bit less invasive than WebApplicationInitializer.

这是与AbstractAnnotationConfigDispatcherServletInitializer配置 servlet 的方式兼容的解决方案。这比WebApplicationInitializer.

It uses an override of AbstractAnnotationConfigDispatcherServletInitializer.customizeRegistration.

它使用AbstractAnnotationConfigDispatcherServletInitializer.customizeRegistration.

public class MySpringWebSetup extends AbstractAnnotationConfigDispatcherServletInitializer
{
  // Your usual obligatory configuration overrides:
  @Override protected Class<?>[] getRootConfigClasses() { ... }
  @Override protected Class<?>[] getServletConfigClasses() { ... }
  @Override protected String[] getServletMappings() { ... }

  // Optional configuration:
  @Override
  protected void customizeRegistration(Dynamic registration) {
    registration.setMultipartConfig(
      // Maybe use more sophisticated configuration than this:
      new MultipartConfigElement("")
    );
  }
}

I found it catching the stack trace of getServletMappingsand thus getting into the code of org\springframework\web\servlet\support\AbstractDispatcherServletInitializer.java:

我发现它捕获了 的堆栈跟踪,getServletMappings从而进入了以下代码org\springframework\web\servlet\support\AbstractDispatcherServletInitializer.java

protected void registerDispatcherServlet(ServletContext servletContext) {

    [more registration stuff was here]

    registration.setLoadOnStartup(1);
    registration.addMapping(getServletMappings());
    registration.setAsyncSupported(isAsyncSupported());

    Filter[] filters = getServletFilters();
    if (!ObjectUtils.isEmpty(filters)) {
        for (Filter filter : filters) {
            registerServletFilter(servletContext, filter);
        }
    }


    customizeRegistration(registration);
}