文件上传报错The current request is not a multipart request;
JSP页面表单:
<form id="upMediaForm" action="${pageContext.request.contextPath}/system/upload/saveUpload" method="post" enctype="multipart/form-data">
<input type="file" name="file"/>
<a href="#" onclick="save();" class="btn btn-primary radius" >确定</a> 
</form>
逻辑代码:
@ResponseBody
@RequestMapping("saveUpload")
public ModelAndView saveUpload(HttpServletRequest request, HttpServletResponse response,@RequestParam("file") MultipartFile file)
throws ServletException, IOException {
String name = "";
if (request instanceof MultipartHttpServletRequest) {
// process
// 判断文件是否为空
if (!file.isEmpty()) {
try {
name = file.getOriginalFilename();
// 文件保存路径
String filePath = request.getSession().getServletContext().getRealPath("/") + "upload/"+new Date().getTime()
+ name;
//如果没有此路径,新建文件夹
File folder = new File(filePath);
if(!folder.exists() || folder.isDirectory()){
folder.mkdirs();
}
// 转存文件
file.transferTo(new File(filePath));
} catch (Exception e) {
e.printStackTrace();
}
}
}
ModelAndView mv = new ModelAndView("/system/install/saveInstall");
request.setAttribute("mediaName", name);
return mv;
}
且本地可以上传,服务器报错
已加载jar包 commons-fileupload-1.2.2.jar
SpringMVC配置:
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8" />
</bean>
相关分类