|
|
|
@ -16,9 +16,13 @@
|
|
|
|
|
|
|
|
|
|
package org.springframework.boot.autoconfigure.web;
|
|
|
|
|
|
|
|
|
|
import javax.servlet.MultipartConfigElement;
|
|
|
|
|
import javax.servlet.Servlet;
|
|
|
|
|
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
|
|
|
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
|
|
|
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
|
|
|
|
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
|
|
|
|
import org.springframework.boot.context.embedded.EmbeddedWebApplicationContext;
|
|
|
|
|
import org.springframework.boot.context.embedded.MultipartConfigFactory;
|
|
|
|
@ -28,58 +32,55 @@ import org.springframework.context.annotation.Configuration;
|
|
|
|
|
import org.springframework.util.StringUtils;
|
|
|
|
|
import org.springframework.web.multipart.support.StandardServletMultipartResolver;
|
|
|
|
|
|
|
|
|
|
import javax.servlet.MultipartConfigElement;
|
|
|
|
|
import javax.servlet.Servlet;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* {@link EnableAutoConfiguration Auto-configuration} for multi-part uploads. Adds a
|
|
|
|
|
* {@link StandardServletMultipartResolver} if none is present, and adds a
|
|
|
|
|
* {@link javax.servlet.MultipartConfigElement multipartConfigElement} if none is otherwise defined.
|
|
|
|
|
* The {@link EmbeddedWebApplicationContext} will associate the
|
|
|
|
|
* {@link javax.servlet.MultipartConfigElement multipartConfigElement} if none is
|
|
|
|
|
* otherwise defined. The {@link EmbeddedWebApplicationContext} will associate the
|
|
|
|
|
* {@link MultipartConfigElement} bean to any {@link Servlet} beans.
|
|
|
|
|
* <p/>
|
|
|
|
|
* The {@link javax.servlet.MultipartConfigElement} is a Servlet API that's used to configure how the container handles
|
|
|
|
|
* file uploads. By default
|
|
|
|
|
* The {@link javax.servlet.MultipartConfigElement} is a Servlet API that's used to
|
|
|
|
|
* configure how the container handles file uploads. By default
|
|
|
|
|
*
|
|
|
|
|
* @author Greg Turnquist
|
|
|
|
|
* @author Josh Long
|
|
|
|
|
*/
|
|
|
|
|
@Configuration
|
|
|
|
|
@ConditionalOnClass({Servlet.class, StandardServletMultipartResolver.class})
|
|
|
|
|
@ConditionalOnClass({ Servlet.class, StandardServletMultipartResolver.class })
|
|
|
|
|
@ConditionalOnExpression("${multipart.enabled:true}")
|
|
|
|
|
@EnableConfigurationProperties(MultipartProperties.class)
|
|
|
|
|
public class MultipartAutoConfiguration {
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private MultipartProperties multipartProperties = new MultipartProperties();
|
|
|
|
|
|
|
|
|
|
@Bean
|
|
|
|
|
@ConditionalOnMissingBean
|
|
|
|
|
public MultipartConfigElement multipartConfigElement() {
|
|
|
|
|
MultipartConfigFactory factory = new MultipartConfigFactory();
|
|
|
|
|
@Autowired
|
|
|
|
|
private MultipartProperties multipartProperties = new MultipartProperties();
|
|
|
|
|
|
|
|
|
|
if (StringUtils.hasText(this.multipartProperties.getFileSizeThreshold())) {
|
|
|
|
|
factory.setFileSizeThreshold(this.multipartProperties.getFileSizeThreshold());
|
|
|
|
|
}
|
|
|
|
|
@Bean
|
|
|
|
|
@ConditionalOnMissingBean
|
|
|
|
|
public MultipartConfigElement multipartConfigElement() {
|
|
|
|
|
MultipartConfigFactory factory = new MultipartConfigFactory();
|
|
|
|
|
|
|
|
|
|
if (StringUtils.hasText(this.multipartProperties.getLocation())) {
|
|
|
|
|
factory.setLocation(this.multipartProperties.getLocation());
|
|
|
|
|
}
|
|
|
|
|
if (StringUtils.hasText(this.multipartProperties.getFileSizeThreshold())) {
|
|
|
|
|
factory.setFileSizeThreshold(this.multipartProperties.getFileSizeThreshold());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (StringUtils.hasText(this.multipartProperties.getMaxRequestSize())) {
|
|
|
|
|
factory.setMaxRequestSize(this.multipartProperties.getMaxRequestSize());
|
|
|
|
|
}
|
|
|
|
|
if (StringUtils.hasText(this.multipartProperties.getLocation())) {
|
|
|
|
|
factory.setLocation(this.multipartProperties.getLocation());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (StringUtils.hasText(this.multipartProperties.getMaxFileSize())) {
|
|
|
|
|
factory.setMaxFileSize(this.multipartProperties.getMaxFileSize());
|
|
|
|
|
}
|
|
|
|
|
if (StringUtils.hasText(this.multipartProperties.getMaxRequestSize())) {
|
|
|
|
|
factory.setMaxRequestSize(this.multipartProperties.getMaxRequestSize());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return factory.createMultipartConfig();
|
|
|
|
|
}
|
|
|
|
|
if (StringUtils.hasText(this.multipartProperties.getMaxFileSize())) {
|
|
|
|
|
factory.setMaxFileSize(this.multipartProperties.getMaxFileSize());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return factory.createMultipartConfig();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Bean
|
|
|
|
|
@ConditionalOnMissingBean
|
|
|
|
|
public StandardServletMultipartResolver multipartResolver() {
|
|
|
|
|
return new StandardServletMultipartResolver();
|
|
|
|
|
}
|
|
|
|
|
@Bean
|
|
|
|
|
@ConditionalOnMissingBean
|
|
|
|
|
public StandardServletMultipartResolver multipartResolver() {
|
|
|
|
|
return new StandardServletMultipartResolver();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|