1 import javax.servlet.Filter; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 5 import org.springframework.boot.autoconfigure.SpringBootApplication; 6 import org.springframework.boot.autoconfigure.web.MultipartAutoConfiguration; 7 import org.springframework.boot.builder.SpringApplicationBuilder; 8 import org.springframework.boot.context.web.SpringBootServletInitializer; 9 import org.springframework.context.annotation.Bean; 10 import org.springframework.transaction.annotation.EnableTransactionManagement; 11 import org.springframework.web.filter.CharacterEncodingFilter; 12 import org.springframework.web.filter.HttpPutFormContentFilter; 13 import org.springframework.web.multipart.commons.CommonsMultipartResolver; 14 15 //exclude表示自动配置时不包括Multipart配置 16 @EnableAutoConfiguration(exclude = {MultipartAutoConfiguration.class}) 17 @SpringBootApplication 18 @EnableTransactionManagement 19 public class Application extends SpringBootServletInitializer { 20 21 @Override 22 protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { 23 return application.sources(Application.class); 24 } 25 26 @Bean 27 public Filter initializeHttpPutHandler() { 28 return new HttpPutFormContentFilter(); 29 } 30 31 @Bean 32 public CharacterEncodingFilter initializeCharacterEncodingFilter() { 33 CharacterEncodingFilter filter = new CharacterEncodingFilter(); 34 filter.setEncoding("UTF-8"); 35 filter.setForceEncoding(true); 36 return filter; 37 } 38 39 @Bean(name = "multipartResolver") 40 public CommonsMultipartResolver multipartResolver() { 41 CommonsMultipartResolver resolver = new CommonsMultipartResolver(); 42 resolver.setDefaultEncoding("UTF-8"); 43 resolver.setResolveLazily(true);// resolveLazily属性启用是为了推迟文件解析 44 resolver.setMaxInMemorySize(40960); 45 resolver.setMaxUploadSize(50 * 1024 * 1024);// 上传文件大小 50M 50*1024*1024 46 return resolver; 47 } 48 49 public static void main(String[] args) throws Exception { 50 SpringApplication.run(Application.class, args); 51 } 52 53 }