一、问题
springboot项目在上传较大文件时报错:
org.apache.tomcat.util.http.fileupload.FileUploadBase$FileSizeLimitExceededException:
The field file exceeds its maximum permitted size of 1048576 bytes.
二、解决
springBoot项目自带的tomcat对上传的文件大小有默认1M限制,
SpringBoot官方文档中展示:
每个文件的配置最大为1Mb,单次请求的文件的总数不能大于10Mb。
直接在在application.properties配置文件中加入如下代码:
# maxFileSize 单个数据大小
spring.servlet.multipart.maxFileSize = 10MB
# maxRequestSize 是总数据大小
spring.servlet.multipart.maxRequestSize=100MB
三、相关代码
layui代码:
<script th:inline="javascript">
ctxPath = /*[[@{/}]]*/ '';
layui.use(["element", "layer", "upload"], function () {
var element = layui.element;
var layer = layui.layer;
var upload = layui.upload;
layui.upload.render({
elem: "#importData",
url: ctxPath + 'xx/uploadFile?id=' + [[${id}]],
size: '10240',
accept: "file",
exts: 'pdf',
before: function () {
layer.load();
},
done: function (result) {
if (result.code === 0) {
layer.msg(result.message, {icon: 1, time: 1000}, function () {
var index = parent.layer.getFrameIndex(window.name);
parent.layer.close(index);
parent.$('.layui-laypage-btn').click();
});
} else if (result.code === -1) {
layer.msg(result.message, {icon: 5, time: 2000}, function () {
var index = parent.layer.getFrameIndex(window.name);
parent.layer.close(index);
parent.$('.layui-laypage-btn').click();
});
}
layer.closeAll('loading');
}
});
});
</script>
后台接口示例:
@ResponseBody
@PostMapping(value = "upload")
public Result upload(@RequestParam MultipartFile file) {
log.info("Excel批量上传认证多选题信息!");
...
int i = service.insertBatch(legalizeList);
if (i > 0) {
log.info("批量导入成功");
return Result.success("导入成功", null);
} else {
log.error("批量导入失败!");
return Result.error("导入失败!");
}
} catch (Exception e) {
log.error("数据导入出现异常:{}", e.getMessage());
return Result.error("数据导入失败,请检查数据格式!");
}
}
有用请点赞,养成良好习惯!
疑问、交流、鼓励请留言!
|