org.apache.tomcat.util.http.fileupload.FileUploadBase$FileSizeLimitExceededException: The field fileContext exceeds its maximum permitted size of 1048576 bytes.] with root cause
意思是:字段fileContext超出其最大允许大小1048576字节。
因为springboot内置tomact的的文件传输默认为1MB,如果不进行配置则会出现以下错误
2019-05-08/13:18:34.006|||||^_^|[http-nio-8080-exec-9] DEBUG org.apache.shiro.web.servlet.SimpleCookie 389 - Found 'JSESSIONID' cookie value [168f49ad-02af-46dc-8862-cfcc4069f2ba]
2019-05-08/13:18:34.017|||||^_^|[http-nio-8080-exec-9] ERROR o.a.c.c.C.[.[localhost].[/].[dispatcherServlet] 175 - Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.web.multipart.MaxUploadSizeExceededException: Maximum upload size exceeded; nested exception is java.lang.IllegalStateException: org.apache.tomcat.util.http.fileupload.FileUploadBase$FileSizeLimitExceededException: The field fileContext exceeds its maximum permitted size of 1048576 bytes.] with root cause
org.apache.tomcat.util.http.fileupload.FileUploadBase$FileSizeLimitExceededException: The field fileContext exceeds its maximum permitted size of 1048576 bytes.
at org.apache.tomcat.util.http.fileupload.FileUploadBase$FileItemIteratorImpl$FileItemStreamImpl$1.raiseError(FileUploadBase.java:633)
at org.apache.tomcat.util.http.fileupload.util.LimitedInputStream.checkLimit(LimitedInputStream.java:76)
at org.apache.tomcat.util.http.fileupload.util.LimitedInputStream.read(LimitedInputStream.java:135)
at java.io.FilterInputStream.read(FilterInputStream.java:107)
at org.apache.tomcat.util.http.fileupload.util.Streams.copy(Streams.java:98)
at org.apache.tomcat.util.http.fileupload.util.Streams.copy(Streams.java:68)
at org.apache.tomcat.util.http.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:293)
at org.apache.catalina.connector.Request.parseParts(Request.java:2855)
at org.apache.catalina.connector.Request.parseParameters(Request.java:3194)
at org.apache.catalina.connector.Request.getParameter(Request.java:1116)
at org.apache.catalina.connector.RequestFacade.getParameter(RequestFacade.java:381)
at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:84)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:200)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:834)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1415)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)
第一种、直接在springboot的启动类里加入下面的代码。
在配置类中配置@Bean,注意当前配置类上需要加注解@Configuration
/**
* 文件上传配置
* @return
*/
@Bean
public MultipartConfigElement multipartConfigElement() {
MultipartConfigFactory factory = new MultipartConfigFactory();
//文件最大10M,DataUnit提供5中类型B,KB,MB,GB,TB
factory.setMaxFileSize(DataSize.of(10, DataUnit.MEGABYTES));
/// 设置总上传数据总大小10M
factory.setMaxRequestSize(DataSize.of(10, DataUnit.MEGABYTES));
return factory.createMultipartConfig();
}
第二种、在application.properties中添加如下配置信息
配置文件直接配置,与springboot2.x版本一致
application.properties
#文件上传大小为20M
spring.servlet.multipart.max-file-size=20MB
#请求大小为20M
spring.servlet.multipart.max-request-size=20MB
application.yml
spring:
servlet:
multipart:
max-file-size: 20MB
max-request-size: 20MB
maxFileSize 是单个文件大小
maxRequestSize是设置总上传的数据大小
这就可以了。
根据自己需求定义吧,只能是MB和KB两种类型,字母大小写随意,Long类型可以的
另外这是MultipartConfigFactory类中的转换,可以瞅一眼
private long parseSize(String size) {
Assert.hasLength(size, "Size must not be empty");
size = size.toUpperCase(Locale.ENGLISH);
if (size.endsWith("KB")) {
return Long.valueOf(size.substring(0, size.length() - 2)) * 1024L;
} else {
return size.endsWith("MB") ? Long.valueOf(size.substring(0, size.length() - 2)) * 1024L * 1024L : Long.valueOf(size);
}
}
原文地址:https://blog.csdn.net/qq_28089993/article/details/76854112