springboot 上傳圖片報錯 檔案大小的問題解決辦法
阿新 • • 發佈:2019-01-01
前面省略N,直接上傳然後報錯程式碼截圖:
nested exception is org.springframework.web.multipart.MultipartException: Could not parse multipart servlet request; nested exception is java.lang.IllegalStateException: org.apache.tomcat.util.http.fileupload.FileUploadBase$FileSizeLimitExceededException: The field upfile exceeds its maximum permitted size of 1048576 bytes.] with root cause org.apache.tomcat.util.http.fileupload.FileUploadBase$FileSizeLimitExceededException: The field upfile exceeds its maximum permitted size of 1048576 bytes. at org.apache.tomcat.util.http.fileupload.FileUploadBase$FileItemIteratorImpl$FileItemStreamImpl$1.raiseError(FileUploadBase.java:633)
我這裡就擷取 幾個重要的報錯資訊了。
解決辦法如下 在springboot中新增 配置資訊
package mview.sys.main.config; import javax.servlet.MultipartConfigElement; import javax.servlet.Servlet; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.web.MultipartProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.multipart.MultipartResolver; import org.springframework.web.multipart.support.StandardServletMultipartResolver; import org.springframework.web.servlet.DispatcherServlet; @Configuration @ConditionalOnClass({ Servlet.class, StandardServletMultipartResolver.class, MultipartConfigElement.class }) @ConditionalOnProperty(prefix = "spring.http.multipart", name = "enabled", matchIfMissing = true) @EnableConfigurationProperties(MultipartProperties.class) public class MultipartAutoConfiguration { @Autowired private MultipartProperties multipartProperties; // public MultipartAutoConfiguration(MultipartProperties // multipartProperties) { // this.multipartProperties = multipartProperties; // } @Bean @ConditionalOnMissingBean public MultipartConfigElement multipartConfigElement() { this.multipartProperties.setMaxFileSize("-1"); return this.multipartProperties.createMultipartConfig(); } @Bean(name = DispatcherServlet.MULTIPART_RESOLVER_BEAN_NAME) @ConditionalOnMissingBean(MultipartResolver.class) public StandardServletMultipartResolver multipartResolver() { StandardServletMultipartResolver multipartResolver = new StandardServletMultipartResolver(); multipartResolver.setResolveLazily(false); return multipartResolver; } }
application.properties
multipart.maxFileSize=10Mb
multipart.maxRequestSize=10Mb