关于SpringMVC中文件上传的问题, enctype="multipart/form-data"
远东HQ 2017-12-13 10:19:16 最近在做公司项目的时候遇到了一个问题,使用SpringMVC为基础的一个框架来做一个文件上传的功能,
源码如下
<body class="white-bg" formid="gyglForm">
<form:form id="gyglForm" modelAttribute="data" method="post" class="form-horizontal" enctype="multipart/form-data" name="theFile">
<form:hidden path="id" />
<table class="table table-bordered table-condensed dataTables-example dataTable no-footer">
<tbody>
<tr>
<td>文件编号:${data.num}</td>
</tr>
<tr>
<td><input type="file" value="上传文件"/></td>
</tr>
</tbody>
</table>
</form:form>
<%-- <form action="onLoadFile" id="gyglForm" class="form-horizontal" method="post" enctype="multipart/form-data" name="theFile">
<form:hidden path="id"/>
<table class="table table-bordered table-condensed dataTables-example dataTable no-footer">
<tbody>
<tr>
<td>文件编号:${data.num}</td>
</tr>
<tr>
<td><input type="file" name="theFile" /></td>
<td><input type="submit" value="提交" class="form-control/></td>
</tr>
</tbody>
</table>
</form> --%>
</body>
后台Controller层
@RequestMapping(value = "{id}/onLoadFile", method = {RequestMethod.POST})
@ResponseBody
public AjaxJson onLoadFile(@PathVariable("id") String id,@RequestParam("theFile")MultipartFile file,HttpServletRequest request,
HttpServletResponse response) {
AjaxJson ajaxJson = new AjaxJson();
ajaxJson.success("文件上传成功");
String str = file.getOriginalFilename();
String url = "E:/JXD_Eclipse/wpms/WebRoot/WEB-INF/webpage/modules/gygl/gyglFile/"+str;
System.out.println(url);
Gygl gygl = commonService.selectById(id);
gygl.setFilepath(url);
commonService.insertOrUpdate(gygl);
try {
file.transferTo(new File(url));
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return ajaxJson;
}
在使用前天页面中,被注释掉的那部分是可以成功的,在controller的方法里也可以接收到数据,但是使用Form:form形式提交的时候,后台总是报he current request is not a multipart request错误,请问这是为什么?