SpringBoot項目上傳文件報錯
阿新 • • 發佈:2018-09-13
msg igel boot exceptio pri color The sts 文件添加
SpringBoot項目上傳圖片報錯 FileSizeLimitExceededException: The field upload exceeds its maximum permitted size of 1048576 bytes.
處理辦法:
1.application.yml文件添加
spring.http.multipart.maxFileSize : 3Mb
spring.http.multipart.maxRequestSize : 100Mb
2.Application.java文件添加
@Bean public MultipartConfigElement multipartConfigElement() { MultipartConfigFactory factory= new MultipartConfigFactory(); //允許上傳的文件最大值 factory.setMaxFileSize("3MB"); //KB,MB /// 設置總上傳數據總大小 factory.setMaxRequestSize("100MB"); return factory.createMultipartConfig(); }
3.Controller文件內添加異常處理
@ExceptionHandler(MultipartException.class) @ResponseBody public String handleAll(){ String errorMsg= "文件過大,不能超過3MB"; return errorMsg; }
註:Controller class文件需添加註解@ControllerAdvice
SpringBoot項目上傳文件報錯