首页 > 代码库 > 排除在“enctype=‘multipart/form-data’”之外的MultipartException

排除在“enctype=‘multipart/form-data’”之外的MultipartException

前不久,在提交一份<form>表单以完成文件上传时,系统报了一个看上去比较常见的错误:

 1 HTTP Status [500] – [Internal Server Error]
 2 
 3 Type Exception Report
 4 
 5 Message Request processing failed; nested exception is org.springframework.web.multipart.MultipartException: Current request is not a multipart request
 6 
 7 Description The server encountered an unexpected condition that prevented it from fulfilling the request.
 8 
 9 Exception
10 
11 org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.web.multipart.MultipartException: Current request is not a multipart request
12 org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:982)
13 org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
14 javax.servlet.http.HttpServlet.service(HttpServlet.java:635)
15 org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
16 javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
17 org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
18 org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197)
19 org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
20 Root Cause
21 
22 org.springframework.web.multipart.MultipartException: Current request is not a multipart request
23 org.springframework.web.method.annotation.RequestParamMethodArgumentResolver.handleMissingValue(RequestParamMethodArgumentResolver.java:190)
24 org.springframework.web.method.annotation.AbstractNamedValueMethodArgumentResolver.resolveArgument(AbstractNamedValueMethodArgumentResolver.java:109)
25 org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:121)
26 org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:160)
27 org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:129)
28 org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:116)
29 org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
30 org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
31 org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
32 org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963)
33 org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897)
34 org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
35 org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
36 javax.servlet.http.HttpServlet.service(HttpServlet.java:635)
37 org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
38 javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
39 org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
40 org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197)
41 org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
42 Note The full stack trace of the root cause is available in the server logs.

 

反复确认后,确定表单中的确存在enctype=‘multipart/form-data‘属性,那么究竟是错在哪里呢?

1、经过排查,数据文件已经上传成功,文件夹和数据库皆有显示,可见CRUD、DAO和Service部分不太可能出现问题。

2、表单中的botton按钮(“type=submit”)可能将form提交多次,故将botton改成input,并用js提交表单。

 1 $("#subBtn").click(function(){
 2   /** 表单输入校验 */
 3   var pic = $("#pic");
 4   var msg = "";
 5 
 6   // 判断表单元素
 7   if (pic.val() == ""){
 8     msg = "商品标题不能为空!";
 9   }
10 
11   if (msg != ""){
12     alert(msg);
13     return;
14   }else{
15     $("#addCarouselFigureForm").submit();
16   }
17 });

经检验,错误依旧。

 

3、在后台方法代码中添加传入参数HttpRequest.HttpMethod,想要获取客户端使用的 HTTP 数据传输方法(如 GET、POST 或 HEAD)。不期返回了一个GET请求无效的错误。尝试失败。

4、最后把怀疑的地方定位到后台方法的return。

一开始代码是return到View视图地址,如下:

1 return "redirect:/admin/carouselFigure/addCarouselFigure";

改成不用redirect的地址(重写了一遍):

1 return "carouselFigure/carouselFigureList";

竟然能顺利跳转(虽然数据没有显示)。

后来一对比,发现是return的地址错误,又再次调用了本方法的RequestMapping请求。

 

总结:

报错大抵是千篇一律的,但是错误的行程却是千奇百怪。

“MultipartException: Current request is not a multipart request”这个错误除了没写“enctype=‘multipart/form-data”属性之外,还有可能是return返回错误。

日后还需多多细心检查。

 

排除在“enctype=‘multipart/form-data’”之外的MultipartException