How does one specify a temp directory for file uploads in Spring Boot?

前端 未结 4 1660
太阳男子
太阳男子 2021-02-01 17:42

I\'m using Spring Boot and need to let users upload files for processing. Right now, the file uploads to /home/username/git/myproject which is not great.

How do I make S

相关标签:
4条回答
  • 2021-02-01 18:12

    Since you are using Spring Boot it's easier to use the MultipartProperties in your application.properties file.

    From documentation properties example:

    # MULTIPART (MultipartProperties)
    multipart.enabled=true
    multipart.file-size-threshold=0 # Threshold after which files will be written to disk.
    multipart.location= # Intermediate location of uploaded files.
    multipart.max-file-size=1Mb # Max file size.
    multipart.max-request-size=10Mb # Max request size.
    

    Also you could read a detailed description from the MultipartProperties.

    In order to configure to your system tmpdir, you could set:

    multipart.location=${java.io.tmpdir}
    
    0 讨论(0)
  • 2021-02-01 18:15

    If somebody is still looking for programmatic configuration:

    @Configuration
    public class ServletConfig {
    
      @Bean
      public ServletRegistrationBean dispatcherRegistration(DispatcherServlet dispatcherServlet) {
        final ServletRegistrationBean registration = new ServletRegistrationBean(dispatcherServlet);
        final String location = System.getProperty("java.io.tmpdir");
        final long maxFileSize = 128*1024;
        final long maxRequestSize = 128*1024;
        final MultipartConfigElement multipartConfig  = new MultipartConfigElement(location, maxFileSize, maxRequestSize, 0);
        registration.setMultipartConfig(multipartConfig);
        return registration;
      }
    
    }
    
    0 讨论(0)
  • 2021-02-01 18:17

    in springboot 1.4.1.RELEASE

    spring.http.multipart.max-file-size=10MB
    spring.http.multipart.max-request-size=10MB
    spring.http.multipart.enabled=true
    spring.http.multipart.location= ..
    

    will be ok.

    0 讨论(0)
  • 2021-02-01 18:19

    On windows versus linux the temp dir could have a trailing slash. Multipart held tmp files that caused new file names. creating my own tmp dir solved the issue.

        String tempDir = System.getProperty("java.io.tmpdir");
        if(  !tempDir.endsWith("/") && !tempDir.endsWith( "\\") ) {
            tempDir = tempDir+"/";
    
    0 讨论(0)
提交回复
热议问题