springmvc上传文件异常

症状:

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="width: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     }
时间: 2024-10-14 16:56:21

springmvc上传文件异常的相关文章

2. SpringMVC 上传文件操作

1.创建java web项目:SpringMVCUploadDownFile 2.在项目的WebRoot下的WEB-INF的lib包下添加如下jar文件 1 com.springsource.com.mchange.v2.c3p0-0.9.1.2.jar 2 com.springsource.net.sf.cglib-2.2.0.jar 3 com.springsource.org.aopalliance-1.0.0.jar 4 com.springsource.org.apache.commo

springmvc上传文件,抄别人的

SpringMVC中的文件上传 分类: SpringMVC 2012-05-17 12:55 26426人阅读 评论(13) 收藏 举报 stringuserinputclassencoding 这是用的是SpringMVC-3.1.1.commons-fileupload-1.2.2和io-2.0.1 首先是web.xml [html] view plaincopyprint? <?xml version="1.0" encoding="UTF-8"?>

SpringMVC上传文件总结

如果是maven项目 需要在pom.xml文件里面引入下面两个jar包 <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>com

SpringMVC上传文件的三种解析方式

springMVC上传文件后,在action解析file文件的三种方式. jsp页面的写法: <form action="parserUploadFile1" method="post" enctype="multipart/form-data"> <input type="file" name="file" /> <input type="submit"

springmvc 上传文件时的错误

使用springmvc上传文件一直失败,文件参数一直为null, 原来是配置文件没写成功. <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- 最大上传尺寸 B单位 1M= 1024*1024 --> <property name="maxUploadSize&

springmvc 上传文件的问题

今天用springmvc 上传文件的时候 报错 org.apache.catalina.connector.RequestFacade cannot be cast to org.springframework.web.multipart.MultipartHttpServletRequest 网上查找原因 说的有如下几种: 1.表单form 上没有 enctype="multipart/form-data"   这个属性 2.配置文件: <bean id="multi

SpringMVC上传文件(图片)并保存到本地

SpringMVC上传文件(图片)并保存到本地 小记一波~ 基本的MVC配置就不展示了,这里给出核心代码 在spring-mvc的配置文件中写入如下配置 <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize">

SpringMVC上传文件的三种方式(转载)

直接上代码吧,大伙一看便知 这时:commonsmultipartresolver 的源码,可以研究一下 http://www.verysource.com/code/2337329_1/commonsmultipartresolver.java.html 前台: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 <%@ page language="java" contentTy

springmvc上传文件方法及注意事项

本文基于注解的配置,敬请留意  基于注解整合 一.springmvc为我们提供两种上传方式配置: 1)org.springframework.web.multipart.commons.CommonsMultipartResolver (基于commons-fileupload.jar) 2) org.springframework.web.multipart.support.StandardServletMultipartResolver (基于servlet3.0+) 二.配置方式基于com