SpingMvc上传文件File遇到的坑爹bug
出bug前的代码片段
<div id="dlg" class="easyui-dialog" style="width: 670px;height: 500px;padding: 10px 20px"
closed="true" buttons="#dlg-buttons">
<form id="fm" method="post" enctype="multipart/form-data">
<table cellspacing="8px;">
<tr>
<td>商品名称:</td>
<td colspan="4"><input class="easyui-validatebox" id="name" name="name" required="true" style="width: 300px;"/></td>
</tr>
<tr>
<td>价格:</td>
<td colspan="4"><input class="easyui-validatebox" id="price" name="price" required="true"/></td>
</tr>
<tr>
<td>库存:</td>
<td colspan="4"><input class="easyui-validatebox" id="stock" name="stock" required="true"/></td>
</tr>
<tr>
<td>商品图片:</td>
<td colspan="4"><input id="pP" name="proPic" type="file"/></td>
</tr>
<tr>
<td>所属大类:</td>
<td colspan="4"><input class="easyui-combobox" id="bName" name="bigType.id" data-options="panelHeight:'auto',editable:false,valueField:'id',textField:'name',url:'${pageContext.request.contextPath}/admin/bigType/comboList.do'"/></td>
</tr>
<tr>
<td>所属小类:</td>
<td colspan="4"><input class="easyui-combobox" id="sName" name="smallType.id" data-options="panelHeight:'auto',editable:false,valueField:'id',textField:'name',url:'${pageContext.request.contextPath}/admin/smallType/comboList.do'" /></td>
</tr>
<tr>
<td valign="top">备注:</td>
<td colspan="4">
<textarea rows="7" cols="50" name="description" id="description"></textarea>
<input type="hidden" id="proPic" name="proPic" />
<input type="hidden" id="hot" name="hot"/>
<input type="hidden" id="specialPrice" name="specialPrice" />
<input type="hidden" id="hotTime" name="hotTime" />
<input type="hidden" id="specialPriceTime" name="specialPriceTime" />
</td>
</tr>
</table>
</form>
</div>
@RequestMapping("/save")
public String save(Product product,@RequestParam(value="proPic",required=false) MultipartFile proPic,HttpServletRequest request,HttpServletResponse response)throws Exception{
if(!proPic.isEmpty()) {
String filePath = request.getServletContext().getRealPath("/");
String imageName=DateUtil.getCurrentDateStr()+"."+proPic.getOriginalFilename().split("\\.")[1];
proPic.transferTo(new File(filePath+"static/images/product/"+imageName));
product.setProPic(imageName);
}
int resultTotal = productService.saveProduct(product);
JSONObject result = new JSONObject();
if(resultTotal>0) {
result.put("success", true);
}else {
result.put("success", false);
}
ResponseUtil.write(response, result);
return null;
}
试了一天一直报错
试了很多方法不管用,后来将proPic改为其他字母image,不与实体类里的字段相同,就能起作用了。