gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:15:06 24 4
gpt4 key购买 nike

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

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...

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.

任何指导将不胜感激。

最佳答案

看起来你需要这个:

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

关于java - 用于 Servlet 3.0 的 MultipartResolver 的 Spring 4 Java 配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23570014/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com