症状:
error:
org.springframework.web.multipart.MultipartException: Current request is not a multipart request
at org.springframework.web.method.annotation.RequestParamMethodArgumentResolver.handleMissingValue(RequestParamMethodArgumentResolver.java:188)
at org.springframework.web.method.annotation.AbstractNamedValueMethodArgumentResolver.resolveArgument(AbstractNamedValueMethodArgumentResolver.java:104)
at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:121)
at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:161)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:128)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:114)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
原因:
jsp中form表单 ajax提交 不能用序列化,序列化方法只能序列化非文件格式的数据 文件格式则不被序列化,后台接收不到文件数据
用 FormData来封装数据
解决方法:
jquery尽量用新版
js:
1 var formData = new FormData($( "#addMemberForm" )[0]); 2 3 4 $.ajax({ 5 type : "POST", 6 url : "<%=basePath%>manage/memberActive/add", 7 data : formData, 8 async: false, 9 cache: false, 10 dataType : "json", 11 contentType: false, 12 processData: false, 13 success: function(result){ 14 console.log(result); 15 if(result){ 16 window.location.href = "<%=basePath%>manage/memberActive/list"; 17 18 }else{ 19 alert(result.msg); 20 } 21 } 22 });
jsp:
1 <form id="addMemberForm" > 2 <div class="loadingActive-left"> 3 <input type="text" name="titel" id="titel" placeholder="请输入标题" ></input> 4 <div class="uploadBox"> 5 <div class="big-photo"> 6 <div id="preview"> 7 <img id="imghead" border="0" src="<%=basePath%>resources/images/upload.jpg" width="270" 8 height="100" onclick="$('#previewImg').click();"></img> 9 </div> 10 <input type="file" onchange="previewImage(this)" 11 style="display: none;" id="previewImg" name="file"></input> 12 </div> 13 </div> 14 <textarea style="100%; height:41%;resize:none" rows="9" cols="75" name="content" id="content" placeholder="请输入内容..." ></textarea> 15 </div> 16 </form>
action:
1 @RequestMapping(value = "add", method = RequestMethod.POST) 2 @ResponseBody 3 public BaseResponse<Boolean> addMemberActivity(HttpServletRequest request, 4 @RequestParam(value = "file", required = false) MultipartFile file) throws IOException { 6 MemberActivity mActivitie = new MemberActivity(); 7 UploadUtils up = new UploadUtils(); 8 String Imgname = file.getOriginalFilename().substring(0, file.getOriginalFilename().length() - 4); 9 String url = up.uploadFile(1, 1, Imgname, file); 10 mActivitie.setStatusId((short) 1); 11 mActivitie.setTitel(request.getParameter("titel")); 12 mActivitie.setCoverUrl(url); 13 mActivitie.setShortDesc("无简介"); 14 mActivitie.setContent(request.getParameter("content")); 15 mActivitie.setTotalPrice(0.01); 16 mActivitie.setDiscountPrice(0.01); 17 mActivitie.setCreateTime(new Timestamp(System.currentTimeMillis())); 18 memberActivityService.addMemberActivity(mActivitie); 19 return new BaseResponse<Boolean>(true); 20 }