首页 > 解决方案 > 迁移到 Spring Boot 时将这段代码放在哪里?

问题描述

我有一个带有此代码的应用程序来调整“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);


    }   

}

现在我正在转向由以下人员创建的 SpringBoot https://start.spring.io/

@SpringBootApplication
public class HadesApplication {

    public static void main(String[] args) {
        SpringApplication.run(HadesApplication.class, args);
    }
}

我想我不会再使用AbstractAnnotationConfigDispatcherServletInitializer了,所以我的旧代码放在哪里?

标签: spring-boot

解决方案


查看该配置,我相信所有设置都可以在application.properties.

https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html

MULTIPART(多部分属性)

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.

推荐阅读