Spring Boot怎么处理上传文件时出现的MultipartException

在config中配置了文件的大小


@Configuration

public class CommonConfig {


    @Bean

    public MultipartConfigElement multipartConfigElement() {

        MultipartConfigFactory factory = new MultipartConfigFactory();

        factory.setMaxFileSize(1024L * 1024L);

        return factory.createMultipartConfig();

    }

}

当文件的大小超出设置值时,返回500错误

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 file exceeds its maximum permitted size of 1048576 bytes.

    at org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.parseRequest(StandardMultipartHttpServletRequest.java:111)

    at org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.<init>(StandardMultipartHttpServletRequest.java:85)

    at org.springframework.web.multipart.support.StandardServletMultipartResolver.resolveMultipart(StandardServletMultipartResolver.java:76)

    at org.springframework.web.servlet.DispatcherServlet.checkMultipart(DispatcherServlet.java:1091)

    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:930)

    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:895)

    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:967)

    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:869)

    at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)

    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:843)

    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)

    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292)

    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)

需要怎么做才能返回200的友好提示?


ITMISS
浏览 8253回答 3
3回答

人到中年有点甜

拦截这个异常:@ControllerAdvicepublic class ExceptionProcess {&nbsp; &nbsp; // 对这个异常的统一处理,返回值 和Controller的返回规则一样&nbsp; &nbsp; @ExceptionHandler(MultipartException.class)&nbsp; &nbsp; public String handleAll(Throwable t){&nbsp; &nbsp; &nbsp; &nbsp; // TODO do sth&nbsp; &nbsp; &nbsp; &nbsp; return "view";&nbsp; &nbsp; }}

翻翻过去那场雪

楼主最后是怎么解决的?我知道这个问题是设置了限制大小之后file穿不过去controller,在表单传过去的时候就被拦截抛异常了,我尝试过2楼的方法拦截了这个异常统一处理,但是控制台将这个异常处理了两次,不明白问题在哪。求助!!源码:Controller:@RequestMapping(value = "/upload",method = RequestMethod.POST)@ResponseBodypublic String upload(@RequestParam("test") MultipartFile file) {&nbsp; &nbsp; String result = fs.upload(file);&nbsp; &nbsp; return result;}Service:@Servicepublic class fileServiceImp implements fileService {@Overridepublic String upload(MultipartFile file) {&nbsp; &nbsp; //判断文件是否为空&nbsp; &nbsp; if (file.isEmpty()) { return "文件为空"; }&nbsp; &nbsp; // 获取文件名&nbsp; &nbsp; String fileName = file.getOriginalFilename();&nbsp; &nbsp; // 获取文件的后缀名&nbsp; &nbsp; String suffixName = fileName.substring(fileName.lastIndexOf("."));&nbsp; &nbsp; //获取文件类型&nbsp; &nbsp; String filetype = file.getContentType();&nbsp; &nbsp; //获取文件大小&nbsp; &nbsp; long size = file.getSize();&nbsp; &nbsp; // 文件上传后的路径&nbsp; &nbsp; String filePath = "/Users/ho/Documents/CMS/src/main/resources/file_temp/";&nbsp; &nbsp; // 解决中文问题,liunx下中文路径,图片显示问题&nbsp; &nbsp; fileName = UUID.randomUUID() + suffixName;&nbsp; &nbsp; File dest = new File(filePath + fileName);&nbsp; &nbsp; // 检测是否存在目录&nbsp; &nbsp; if (!dest.getParentFile().exists()) {&nbsp; &nbsp; &nbsp; &nbsp; dest.getParentFile().mkdirs();&nbsp; &nbsp; }&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; //上传&nbsp; &nbsp; &nbsp; &nbsp; file.transferTo(dest);&nbsp; &nbsp; &nbsp; &nbsp; //获取当前时间&nbsp; &nbsp; &nbsp; &nbsp; Date d = new Date();&nbsp; &nbsp; &nbsp; &nbsp; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");&nbsp; &nbsp; &nbsp; &nbsp; String today = sdf.format(d);&nbsp; &nbsp; &nbsp; &nbsp; //上传后输出上传成功的时间&nbsp; &nbsp; &nbsp; &nbsp; return today + "-" + fileName + "-" + "上传成功";&nbsp; &nbsp; }catch (IllegalStateException e) {&nbsp; &nbsp; &nbsp; &nbsp; return "文件过大,内存溢出异常";&nbsp; &nbsp; }catch (IOException e) {&nbsp; &nbsp; &nbsp; &nbsp; return "文件路径错误,IO异常";&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java