今天测试反馈一卡多号业务上传文件时超过 10M 时就失败。记录一下这个问题。
刚开始看到是请求直接断开,后台也没有日志,猜测是 Nginx 的问题,看了下配置文件,果然是。Nginx 当前设置为 8M,
```
client_max_body_size 8M;
client_body_buffer_size 128k;
```
测试提到文件大小为 20M,修改 Nginx 配置后,发现还是上传失败,这次后台有报错日志了。
报错为:the request was rejected because its size (12047463) exceeds the configured maximum (10485760)
```
org.springframework.web.util.NestedServletException: Request processing failed; 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$SizeLimitExceededException: the request was rejected because its size (12047463) exceeds the configured maximum (10485760)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:982)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
```
看了下我们的配置文件
```
spring.http.multipart.maxFileSize =10Mb
spring.http.multipart.maxRequestSize=10Mb
```
果然是限制了 10M大小,将其修改为 20M,重启应用,圆满解决~
这次发现了限制文件大小的地方还是挺多的,比如前端就可以限制、请求后经过 Nginx 也可以进行限制、还有 Spring Boot 本身也可以通过配置来限制,最后我们对应的处理代码里也可以进行判断来限制文件的大小。
# 2022-01-18 更新
**Spring Boot1.4版本后配置更改为:**
```
spring.http.multipart.maxFileSize = 10Mb
spring.http.multipart.maxRequestSize=100Mb
```
**Spring Boot2.0之后的版本配置修改为:**
```
spring.servlet.multipart.max-file-size = 10MB
spring.servlet.multipart.max-request-size=100MB
```
>记得是 `10MB`,写 `10M` 会报错。。。。。

SpringBoot 上传文件报错,文件大小超过限制