先是controller代码:
@ResponseBody
@RequestMapping("/sumimg.do")
public Result upload(HttpServletRequest request) {
Result result = new Result();
// 转换为文件类型的request
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
// 获取对应file对象
Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
Iterator<String> fileIterator = multipartRequest.getFileNames();
// 获取项目的相对路径(http://localhost:8080/file)
String path = request.getSession().getServletContext().getRealPath("up-img");
while (fileIterator.hasNext()) {
String fileKey = fileIterator.next();
// 获取对应文件
MultipartFile multipartFile = fileMap.get(fileKey);
if (multipartFile.getSize() != 0L) {
String imgName = Util.createId() + multipartFile.getOriginalFilename();
try {
multipartFile.transferTo(new File(path,imgName));
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
result.setData(path+"File.separator"+imgName);
result.setMsg("上传成功!");
result.setStatus(0);
System.out.println(path+"\\"+imgName);
} else {
result.setMsg("上传失败!");
result.setStatus(1);
}
}
return result;
}
然后是spring mvc的配置:
<!-- 使用CommonsMultipartResolver配置一个MultipartResolver解析器 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!--文件最大值 -->
<property name="maxUploadSize" value="1048576000" />
<property name="defaultEncoding" value="utf-8"/>
<!--缓存大小 -->
<property name="maxInMemorySize" value="40960"/>
</bean>
然后是前端的代码:
<script type="text/javascript">
$(function(){
$(".summernote").summernote({
lang:"zh-CN",height:"700px",
callbacks: {
onImageUpload: function(files) {
sendFile(files[0]);
}
}
});
function sendFile(file) {
var data = new FormData();
data = new FormData();
data.append("file", file);
$.ajax({
data: data,
type: "POST",
url: path+"/upload/sumimg.do",
cache: false,
contentType: false,
processData: false,
success: function(result) {
alert(1);
alert(result.data);
},
error: function() {
$(".note-alarm").html("上传失败");
setTimeout(function() {
$(".note-alarm").remove();
},3000);
}
});
}
});
</script>
</head>
<body>
<div class="container summernote" id="summernote"></div>
</body>
最后是所报的错误:
严重: Servlet.service() for servlet springmvc threw exception
java.lang.ClassCastException: org.apache.catalina.connector.RequestFacade cannot be cast to org.springframework.web.multipart.MultipartHttpServletRequest
at cn.xaut.bz.controller.ImageController.upload(ImageController.java:38)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:745)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:686)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:953)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:844)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:829)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:745)
注:图片可以正常的保存到img-upload中,路径也可以正常打印