springboot2.x设置文件上传大小限制,并处理异常

0.关联文章:https://editor.csdn.net/md/?articleId=105131618

1.代码
方法一,通过配置文件

# 开启文件上传
spring.servlet.multipart.enabled=true
# 单个文件上传最大值
spring.servlet.multipart.max-file-size=1MB
# 单个请求最大限制
spring.servlet.multipart.max-request-size=1MB

方法二,通过配置Bean

package com.hcr;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.util.unit.DataSize;

import javax.servlet.MultipartConfigElement;


@SpringBootApplication
public class HcrApplication {
     

    public static void main(String[] args) {
     
        SpringApplication.run(HcrApplication.class, args);
    }

    @Bean
    public MultipartConfigElement multipartConfigElement() {
     
        MultipartConfigFactory factory = new MultipartConfigFactory();
        //单个文件最大
        factory.setMaxFileSize(DataSize.ofMegabytes(5));
        /// 设置总上传数据总大小
        factory.setMaxRequestSize(DataSize.ofMegabytes(5));
        return factory.createMultipartConfig();
    }
}

2.异常处理类

package com.hcr.exception;

import org.apache.tomcat.util.http.fileupload.impl.FileSizeLimitExceededException;
import org.apache.tomcat.util.http.fileupload.impl.SizeLimitExceededException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.servlet.MultipartProperties;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartException;

import java.util.HashMap;
import java.util.Map;

@ControllerAdvice
public class MulipartExceptionHandler {
     
    @Autowired
    private MultipartProperties multipartProperties;

    @ExceptionHandler(value = MultipartException.class)
    @ResponseBody
    public Object dealMulipartException(MultipartException e) {
     
        Map result = new HashMap<>(2);
        String msg = "文件大小超过限制";
        Throwable cause = e.getCause().getCause();
        if (cause instanceof FileSizeLimitExceededException) {
     
            msg = "单个上传文件大小不能超过" + multipartProperties.getMaxFileSize();
        } else if (cause instanceof SizeLimitExceededException) {
     
            msg = "上传文件总大小不能超过" + multipartProperties.getMaxRequestSize();
        }
        result.put("code", "1");
        result.put("data", msg);
        return result;
    }
}

3.文章参考链接
a. https://blog.csdn.net/qq_36515220/article/details/93361119?depth_1-utm_source=distribute.pc_relevant.none-task-blog-OPENSEARCH-1&utm_source=distribute.pc_relevant.none-task-blog-OPENSEARCH-1

b. https://blog.csdn.net/bebmwnz/article/details/90200765?depth_1-utm_source=distribute.pc_relevant.none-task-blog-OPENSEARCH-2&utm_source=distribute.pc_relevant.none-task-blog-OPENSEARCH-2

c. https://blog.csdn.net/qq_29229567/article/details/84939996

d. https://xdclass.net/#/index

你可能感兴趣的:(springboot,spring,boot)