我有一个带有以下代码的应用程序来调整“ MultipartConfigElement”:
public class AppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
private int maxUploadSizeInMb = 5 * 1024 * 1024; // 5 MB
@Override
protected Class<?>[] getRootConfigClasses () {
return null;
}
@Override
protected Class<?>[] getServletConfigClasses () {
return new Class<?>[]{ WebConfig.class };
}
@Override
protected String[] getServletMappings () {
return new String[]{"/"};
}
@Override
protected void customizeRegistration(ServletRegistration.Dynamic registration) {
File uploadDirectory = new File(System.getProperty("java.io.tmpdir"));
MultipartConfigElement multipartConfigElement =
new MultipartConfigElement(uploadDirectory.getAbsolutePath(),
maxUploadSizeInMb, maxUploadSizeInMb * 2, maxUploadSizeInMb / 2);
registration.setMultipartConfig(multipartConfigElement);
}
}
现在我要移至https://start.spring.io/
创建的SpringBoot:
@SpringBootApplication
public class HadesApplication {
public static void main(String[] args) {
SpringApplication.run(HadesApplication.class, args);
}
}
我想我将不再使用AbstractAnnotationConfigDispatcherServletInitializer
,所以将旧代码放在哪里?
答案 0 :(得分:1)
查看该配置,我相信所有设置都可以在application.properties
中完成。
https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html
spring.servlet.multipart.enabled=true # Whether to enable support of multipart uploads.
spring.servlet.multipart.file-size-threshold=0 # Threshold after which files are written to disk. Values can use the suffixes "MB" or "KB" to indicate megabytes or kilobytes, respectively.
spring.servlet.multipart.location= # Intermediate location of uploaded files.
spring.servlet.multipart.max-file-size=1MB # Max file size. Values can use the suffixes "MB" or "KB" to indicate megabytes or kilobytes, respectively.
spring.servlet.multipart.max-request-size=10MB # Max request size. Values can use the suffixes "MB" or "KB" to indicate megabytes or kilobytes, respectively.
spring.servlet.multipart.resolve-lazily=false # Whether to resolve the multipart request lazily at the time of file or parameter access.
答案 1 :(得分:0)
1)实际上,如果您依赖于DispatcherServlet
入门程序,它将使用标准值初始化spring-boot-starter-web
,那么实际上不再需要注册DispatcherServlet
。
2)关于指定的Servlet应用程序上下文的配置:
@Override
protected Class<?>[] getServletConfigClasses () {
return new Class<?>[]{ WebConfig.class };
}
您应将此代码移至WebMvcConfigurer
实现中,例如:
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
....
}
3)关于多部分配置,您可以使用Spring Boot application.properties
进行设置:
# MULTIPART (MultipartProperties)
spring.servlet.multipart.enabled=true
#是否启用对 分段上传。
spring.servlet.multipart.max-file-size=5MB
#最大文件大小。值可以 使用后缀“ MB”或“ KB”表示兆字节或千字节, 分别。
spring.servlet.multipart.location=${java.io.tmpdir}
#上传的中间位置 文件。
这是实际的properties reference。