我采用全Java方法进行Spring MVC配置,无法弄清楚如何以编程方式将MultipartConfigElement
与DispatcherServlet
相关联。
Spring文档声明:
为了使用基于Servlet 3.0的多部分解析,您需要标记 带有" multipart-config"的DispatcherServlet web.xml中的部分,或 在程序化Servlet中使用一个javax.servlet.MultipartConfigElement 登记...
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或任何接受它的构造函数。
另请注意,我的WebConfig声明了MultipartResolver
:
@Bean
public StandardServletMultipartResolver multipartResolver(){
return new StandardServletMultipartResolver();
}
但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.
非常感谢任何指导。
答案 0 :(得分:11)
看起来你需要这个:
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));
答案 1 :(得分:1)
这是与AbstractAnnotationConfigDispatcherServletInitializer
配置servlet方式兼容的解决方案。这比WebApplicationInitializer
的侵入性小。
它使用覆盖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("")
);
}
}
我发现它抓住了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);
}