1. 程式人生 > >排除在“enctype=‘multipart/form-data’”之外的MultipartException

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

scrip car self orm ddc oot expected 元素 返回

前不久,在提交一份<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