问题场景:使用nginx做代理转发一个tomcat发布的springBoot web应用,上传图片时在service层做了图片大小2M的校验,但是上传图片时出现以上两个错误。
问题1:nginx 413 Request Entity Too Large
问题分析:从错误信息判断是当前上传的图片超过了nginx的请求体设置的最大值,查看nginx文档,在核心模块中有关于请求体设置的说明,http://www.nginx.cn/doc/standard/httpcore.html
果然你在官网的说明文档中查到相关说明。然后在核心模块中http{}中配置即可。
问题2 The field uploadFiles exceeds its maximum permitted size of 1048576 bytes
问题分析:在修改了niginx配置后出现了上述问题,完整的错误信息是
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 uploadFiles exceeds its maximum permitted size of 1048576 bytes.
查了源代码是springboot内置的tomcat中发现上传文件大小默认为1M,springboot提供了配置项
spring: http: multipart: max-file-size: 2MB
在application.yml中设置即可。