SpringBoot设置文件上传大小限制--默认为1M,前端传json
1.前端的访问请求
<script type="text/javascript"> $(document).ready(function(){ var saveDataAry=[]; var data1={"userName":"test","address":"gz"}; var data2={"userName":"ququ","address":"gr"}; saveDataAry.push(data1); saveDataAry.push(data2); $.ajax({ type:"POST", url:"user/saveUser", dataType:"json", contentType:"application/json", data:JSON.stringify(saveData), success:function(data){ } }); }); </script>
2.前端的访问请求
// 方式一: @RequestMapping(value = "/saveUser", method = {RequestMethod.POST }}) @ResponseBody public void saveUser(@RequestBody List<User> users) { userService.batchSave(users); } // 方式二: @RequestMapping(value = "/saveUser", method = {RequestMethod.POST }}) @ResponseBody public void saveUser(@RequestBody List<Map<Object, Object>> maps) { userService.batchSave(users); }
3.其他
[1].@RequestBody可以处理一下格式:
application/json
application/xml
[2].multipart/form-data的数据格式不能使用@RequestBody 处理
SpringBoot设置文件上传大小限制--默认为1M
SpringBoot默认上传文件大小不能超过1MB,超过之后会报以下异常:
org.apache.tomcat.util.http.fileupload.FileUploadBase$FileSizeLimitExceededException: The field file exceeds its maximum permitted size of 1048576 bytes.
解决方法:
请在配置文件(application.properties/application.yml)中加入如下设置即可
低版本: 1.X
spring.http.multipart.max-file-size=10MB
spring.http.multipart.max-request-size=10MB
高版本: 2.X
spring.servlet.multipart.max-file-size=30Mb
spring.servlet.multipart.max-request-size=30Mb