springboot springMVC上传文件报错:Failed to parse multipart servlet request

在线助手博客首页

相关报错信息

  • nested exception is java.lang.NoSuchFieldError: MULTIPARTFORMDATA
  • at org.eclipse.jetty.server.Request.getParts(Request.java:2316) ~[jetty-server-9.4.10.RC1.jar:9.4.10.RC1]
  • Failed to parse multipart servlet request

解决办法

一、添加commons-fileupload依赖

 <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.1</version>
</dependency>

二、显示声明CommonsMultipartResolver为mutipartResolver

可以在SpringApplication中配置

//文件上传 显示声明CommonsMultipartResolver为mutipartResolver
    @Bean(name = "multipartResolver")
    public MultipartResolver multipartResolver() {
        CommonsMultipartResolver resolver = new CommonsMultipartResolver();
        resolver.setDefaultEncoding("UTF-8");
        resolver.setResolveLazily(true);//resolveLazily属性启用是为了推迟文件解析,以在UploadAction中捕获文件大小异常
        resolver.setMaxInMemorySize(40960);
        resolver.setMaxUploadSize(5 * 1024 * 1024);//上传文件大小 100M 100*1024*1024
        return resolver;
    }

扫一扫,体验小程序

线助手小程序
-
-
-
Copyright © 2017-2018 . All Rights Reserved.

粤ICP备18008506号