Spring MVC控制器文件上传报错:未提供多部件配置无法处理请求
文件上传Spring MVC异常排查与解决
问题现象
点击注册按钮上传文件时抛出500异常,错误信息如下:
HTTP Status 500 – Internal Server Error Type Exception Report Message Failed to parse multipart servlet request Exception org.springframework.web.multipart.MultipartException: Failed to parse multipart servlet request ............ Root Cause java.lang.IllegalStateException: Unable to process parts as no multi-part configuration has been provided........
操作过程中偶尔还会触发404错误。
现有代码与配置
控制器代码
@Controller @MultipartConfig public class RegistrationController extends HttpServlet { @Autowired public EmployeeService employeeService; @Controller @MultipartConfig public class RegistrationController extends HttpServlet{ @RequestMapping(value="/register", method=RequestMethod.GET) public ModelAndView mav = new ModelAndView("register"); mav.addObject("employee", new Employee()); return mav; } @RequestMapping(value="/register", method=RequestMethod.POST) public ModelAndView addUser(HttpServletRequest request, HttpServletResponse response, @ModelAttribute("employee") Employee employee, @RequestParam MultipartFile photo){ employeeService.register(employee, photo); return new ModelAndView("register","msg","Employee registered successfully"); } }
JSP页面代码(register.jsp)
<form:form id="regForm" modelAttribute="employee" action="registerProcess" method="post" enctype='multipart/form-data'> ........... <tr> <td><form:label path="photo">Employee Photo</form:label></td> <td><form:input type="file" path="photo" name="photo" id="photo" /></td> </tr> <tr> <td></td> <td><form:button class="btn btn-primary" id="register" name="register">Register</form:button></td> </tr> </table> </form:form>
web.xml配置
<filter> <filter-name>MultipartFilter</filter-name> <filter-class>org.springframework.web.multipart.support.MultipartFilter</filter-class> </filter> <filter-mapping> <filter-name>MultipartFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
application.properties配置
spring.servlet.multipart.max-file-size=10MB spring.servlet.multipart.max-request-size=10MB
resources/context.xml配置
<Context allowCasualMultipartParsing="true"></Context>
解决方案
- 修复控制器代码错误:代码存在重复定义类、GET方法语法错误问题,且Spring MVC控制器无需继承
HttpServlet,@Controller注解已完成相关封装,修正后的代码如下:
@Controller public class RegistrationController { @Autowired private EmployeeService employeeService; @RequestMapping(value="/register", method=RequestMethod.GET) public ModelAndView showRegisterForm() { ModelAndView mav = new ModelAndView("register"); mav.addObject("employee", new Employee()); return mav; } @RequestMapping(value="/register", method=RequestMethod.POST) public ModelAndView addUser(@ModelAttribute("employee") Employee employee, @RequestParam("photo") MultipartFile photo){ employeeService.register(employee, photo); return new ModelAndView("register","msg","Employee registered successfully"); } }
- 修正表单提交路径:JSP表单的
action属性为registerProcess,但控制器POST映射路径是/register,路径不匹配导致404,修改form的action为register:
<form:form id="regForm" modelAttribute="employee" action="register" method="post" enctype='multipart/form-data'>
- 移除冗余配置:Spring MVC无需使用
@MultipartConfig注解(这是Servlet原生注解,Spring已封装自身Multipart处理逻辑);若使用Spring Boot,MultipartFilter不是必需配置,可直接移除web.xml中的相关内容,Spring会自动处理Multipart配置。 - 配置MultipartResolver(传统Spring MVC场景):如果是传统Spring MVC项目(非Spring Boot),需在Spring配置文件中添加MultipartResolver的bean:
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" value="10485760"/> <!-- 对应10MB --> </bean>
Spring Boot环境下则无需手动配置,spring.servlet.multipart.*配置会自动生效。
- 检查依赖完整性:Spring Boot项目确保引入
spring-boot-starter-web依赖;传统Spring MVC项目需引入commons-fileupload和commons-io依赖,保证Multipart处理的相关类可用。
内容的提问来源于stack exchange,提问作者userV
相关产品推荐
相关产品推荐

