SpringBoot设置全局捕获MaxUploadSizeExceededException异常

4 篇文章 0 订阅
3 篇文章 0 订阅

需求:在Spring Boot项目中对来自前端的文件上传请求做大小限制,限制文件大小在20mb以内,超出则提示“文件大于20mb”

自定义全局异常 相应代码
@ExceptionHandler({MaxUploadSizeExceededException.class})
    public JsonResult maxUploadSizeExceededException(MaxUploadSizeExceededException maxUploadSizeExceededException) {
        log.info("=====================文件大于50mb====================");
        log.info(maxUploadSizeExceededException+"");
        return new JsonResult("40075","文件大于50mb");
    }

yml配置

spring:
  servlet:
    multipart:
      max-file-size: 20mb
      max-request-size: 20mb

结果报错

org.springframework.web.multipart.MaxUploadSizeExceededException:
 Maximum upload size exceeded; nested exception is java.lang.IllegalStateException: 
org.apache.tomcat.util.http.fileupload.FileUploadBase$SizeLimitExceededException: 
the request was rejected because its size (10935680) exceeds the configured maximum (10485760)
百度好多资料,说是超出tomcat内置的文件大小设置异常会先于controller触发导致异常无法捕获
按照百度提供的解决方案:配置yml
spring:
  servlet:
    multipart:
      enabled: true #默认支持文件上传
      max-file-size: 20mb
      max-request-size: 20mb
      resolve-lazily: true #设置为懒加载,不然依然会捕获不到异常
    tomcat:
      max-swallow-size: 100MB #最重要的是这行

结果依然不可以,有一点奇怪的是

在这里插入图片描述

这里可以打印,说明可以触发异常的,都是前端响应结果

在这里插入图片描述

前端使用的是Vue,axios

this.$axios({
                    method:"post",
                    url:"/imgUpDown",
                    data:formData,
                    headers: {'Content-Type': 'multipart/form-data'}
                }).then(res => {
                    console.log("响应成功:服务端返回值为:",res)
                    
                }).catch(e=>{
                    console.log("请求失败:错误信息:",e)
                   
                })

前端就是不响应自己封装的结果

…三个小时以后。。。

来自厕所的灵感

不限制文件上传大小,自己判断文件大小

yml配置文件

spring:
  servlet:
    multipart:
      enabled: true #默认支持文件上传
      max-file-size: -1
      max-request-size: -1
      resolve-lazily: true #设置为懒加载,不然依然会捕获不到异常
    tomcat:
      max-swallow-size: 100MB #最重要的是这行

自己判断代码

@RequestMapping(value = "imgUpDown",method = {RequestMethod.POST})
    @CrossOrigin
    @ApiOperation(value = "文件上传")
    public JsonResult imgUpDown(@RequestPart("file") MultipartFile file) throws IOException {

        if (file.isEmpty()) return new JsonResult("40072","文件不能为空");

        System.out.println(file.getSize() / (1024*1024));
        if ((file.getSize() / (1024*1024)) > 20 ) return new JsonResult("40073","文件大于20mb");

        String imgUrl = ImgUpDown.imgUpDown(file, domainName.getDomainName());
        Img img = new Img();
        img.setImgUrl(imgUrl);
        imgService.saveImg(img);
        return new JsonResult(imgUrl,"200","图片上传成功");
    }

成功

在这里插入图片描述

事实再次证明:只要思想不滑坡,方法总比困难多

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

炸鸡叔老白

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值