用于Servlet 3.0的MultipartResolver的Spring 4 Java Config [英] Spring 4 Java Config for MultipartResolver for Servlet 3.0

查看:134
本文介绍了用于Servlet 3.0的MultipartResolver的Spring 4 Java Config的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在采用全Java方法进行Spring MVC配置,并且无法弄清楚如何将 MultipartConfigElement 与我的 DispatcherServlet 以编程方式。

I'm taking an all-Java approach to Spring MVC configuration and cannot figure out how to associate a MultipartConfigElement with my DispatcherServlet programmatically.

Spring文档说明:

Spring documentation states:


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

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

这是我的 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("/*");
    }

}

如何关联 MultipartConfigElement 我的 DispatcherServlet ?我没有看到任何方法,如setMultipartConfiguration或任何接受它的构造函数。

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

另请注意,我的WebConfig声明 MultipartResolver

Also note that my WebConfig declares a MultipartResolver:

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

但Spring文档指出:

But the Spring documentation states:

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.

推荐答案

看起来你需要这个:

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

这篇关于用于Servlet 3.0的MultipartResolver的Spring 4 Java Config的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆