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:
var formData = new FormData($( "#addMemberForm" )[0]);
$.ajax({
type : "POST",
url : "<%=basePath%>manage/memberActive/add",
data : formData,
async: false,
cache: false,
dataType : "json",
contentType: false,
processData: false,
success: function(result){
console.log(result);
if(result){
window.location.href = "<%=basePath%>manage/memberActive/list";
}else{
alert(result.msg);
}
}
});
jsp:
<form id="addMemberForm" >
<div class="loadingActive-left">
<input type="text" name="titel" id="titel" placeholder="请输入标题" ></input>
<div class="uploadBox">
<div class="big-photo">
<div id="preview">
<img id="imghead" border="0" src="<%=basePath%>resources/images/upload.jpg" width="270"
height="100" onclick="$('#previewImg').click();"></img>
</div>
<input type="file" onchange="previewImage(this)"
style="display: none;" id="previewImg" name="file"></input>
</div>
</div>
<textarea style="width:100%; height:41%;resize:none" rows="9" cols="75" name="content" id="content" placeholder="请输入内容..." ></textarea>
</div>
</form>
action:
@RequestMapping(value = "add", method = RequestMethod.POST)
@ResponseBody
public BaseResponse<Boolean> addMemberActivity(HttpServletRequest request,
@RequestParam(value = "file", required = false) MultipartFile file) throws IOException {
MemberActivity mActivitie = new MemberActivity();
UploadUtils up = new UploadUtils();
String Imgname = file.getOriginalFilename().substring(0, file.getOriginalFilename().length() - 4);
String url = up.uploadFile(1, 1, Imgname, file);
mActivitie.setStatusId((short) 1);
mActivitie.setTitel(request.getParameter("titel"));
mActivitie.setCoverUrl(url);
mActivitie.setShortDesc("无简介");
mActivitie.setContent(request.getParameter("content"));
mActivitie.setTotalPrice(0.01);
mActivitie.setDiscountPrice(0.01);
mActivitie.setCreateTime(new Timestamp(System.currentTimeMillis()));
memberActivityService.addMemberActivity(mActivitie);
return new BaseResponse<Boolean>(true);
}
springmvc上传文件异常的更多相关文章
- springmvc上传文件,抄别人的
SpringMVC中的文件上传 分类: SpringMVC 2012-05-17 12:55 26426人阅读 评论(13) 收藏 举报 stringuserinputclassencoding 这是 ...
- 2. SpringMVC 上传文件操作
1.创建java web项目:SpringMVCUploadDownFile 2.在项目的WebRoot下的WEB-INF的lib包下添加如下jar文件 com.springsource.com.mc ...
- SpringMVC上传文件总结
如果是maven项目 需要在pom.xml文件里面引入下面两个jar包 <dependency> <groupId>commons-fileupload</groupId ...
- 使用springMVC上传文件
control层实现功能: @RequestMapping(value="upload2") public String upLoad2(HttpServletRequest re ...
- SpringMVC上传文件(图片)并保存到本地
SpringMVC上传文件(图片)并保存到本地 小记一波~ 基本的MVC配置就不展示了,这里给出核心代码 在spring-mvc的配置文件中写入如下配置 <bean id="multi ...
- SpringMVC 上传文件 MultipartFile 转为 File
在使用 SpringMVC 上传文件时,接收到的文件格式为 MultipartFile,但是在很多场景下使用都需要File格式的文件,记录下以便日后使用. 以下mFile为MultipartFile文 ...
- springmvc 上传文件时的错误
使用springmvc上传文件一直失败,文件参数一直为null, 原来是配置文件没写成功. <bean id="multipartResolver" class=" ...
- springmvc上传文件报错org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.web.multipart.MultipartFile]
在用springmvc+mybatis进行项目开发时,上传文件抛异常... org.springframework.beans.BeanInstantiationException: Could no ...
- SpringMVC上传文件的三种方式(转)
直接上代码吧,大伙一看便知 这时:commonsmultipartresolver 的源码,可以研究一下 http://www.verysource.com/code/2337329_1/common ...
随机推荐
- Openstack_SQLAlchemy_一对多关系表的多表插入实现
目录 目录 Openstack 与 SQLAlchemy 一个多表插入的 Demo 小结 Openstack 与 SQLAlchemy SQLAlchemy 是 Python 语言下的一款开源软件,它 ...
- Week3 - 397. Integer Replacement
Week3 - 397. Integer Replacement 397.Integer Replacement - Medium Given a positive integer n and you ...
- Beetle简单构建TCP服务
使用Beetle构建TCP服务应用是件非常简单的事情,它并不需要你去关注Socket细节,如果你想用Socket编写高性能的TCP服务,那你要关注的东西非常多,异步数据处理,大量连接下的线程管理和连接 ...
- unity编辑器Hierarchy添加图标
效果 素材 using UnityEditor; using UnityEngine; using System.Collections.Generic; [InitializeOnLoad] cla ...
- 执行 bower -v 时出现内部错误
安装nodejs ,我的位置是D:\node.js_install.全局模块安装默认放在C:\Users\Administrator\AppData\Roaming\npm\node_modules里 ...
- [Udemy] ES 7 and Elastic Stack - part 2
Section 3: Searching with Elasticsearch query with json 分页返回 Sort full text 的内容不能用来sort, 比如movie的 ti ...
- MVC Html.AntiForgeryToken(); 防止跨站伪造请求(建议所有表单提交都加这个)
视图页面from表单中添加 @Html.AntiForgeryToken(); 然后每个表单提交的时候都会带__RequestVerificationToken 字段 后端控制器验证时添加 [Val ...
- web 前端2 CSS
CSS CSS是Cascading Style Sheets的简称,中文称为层叠样式表,用来控制网页数据的表现,可以使网页的表现与数据内容分离. 一 css的四种引入方式 1.行内式 ...
- ### Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.builder.BuilderException: Error creating document instance. Cause: org.xml.sax.
在做多表映射查询时,在同一个resultMap中写了1:1映射和1:n映射,结果测试时报错如下: org.apache.ibatis.exceptions.PersistenceException: ...
- 谈一下你对 uWSGI 和 nginx 的理解??
1.uWSGI 是一个 Web 服务器,它实现了 WSGI 协议.uwsgi.http 等协议.Nginx 中HttpUwsgiModule 的作用是与 uWSGI 服务器进行交换.WSGI 是一种 ...