在springmvc.xml中配置multipart类型解析器
<!-- 配置multipart类型解析器 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 设置上传文件的最大尺寸为5MB -->
<property name="maxUploadSize">
<value>5242880</value>
</property>
</bean>
导入上传图片的jar包

form表单代码
<form id="itemForm" action="${pageContext.request.contextPath }/items/editItemsSubmit.action" method="post" enctype="multipart/form-data">
<input type="hidden" name="id" value="${itemsCustom.id }"/>
修改商品信息:
<table width="100%" border=1>
<tr>
<td>商品名称</td>
<td><input type="text" name="itemsCustom.name" value="${itemsCustom.name }"/></td>
</tr>
<tr>
<td>商品价格</td>
<td><input type="text" name="itemsCustom.price" value="${itemsCustom.price }"/></td>
</tr>
<tr>
<td>商品生产日期</td>
<td><input type="text" name="itemsCustom.createtime" value="<fmt:formatDate value="${itemsCustom.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/>"/></td>
</tr>
<tr>
<td>商品图片</td>
<td>
<c:if test="${itemsCustom.pic !=null}">
<img src="/pic/${itemsCustom.pic}" width=100 height=100/>
<br/>
</c:if>
<input type="file" name="items_pic"/>
</td>
</tr>
<tr>
<td>商品简介</td>
<td>
<textarea rows="3" cols="30" name="itemsCustom.detail">${itemsCustom.detail }</textarea>
</td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="提交"/>
</td>
</tr>
</table>
</form>
controller代码
//商品信息修改提交
@RequestMapping("/editItemsSubmit")
public String editItemsSubmit(Integer id,ItemsQueryVo itemsQueryVo,MultipartFile items_pic) throws Exception{
//存储图片的物理路径
String pic_path = "F:\\SpringMVC_PROJ\\SpringMVC_pic\\";
//图片文件的原始名
String pictureFile_name = items_pic.getOriginalFilename();
if(items_pic != null && pictureFile_name != null && pictureFile_name.length()>0){
//图片文件的新名称
String newFileName = UUID.randomUUID().toString() + pictureFile_name.substring(pictureFile_name.lastIndexOf("."));
//新图片
File newFile = new File(pic_path+newFileName);
//将内存中的数据写入磁盘
items_pic.transferTo(newFile);
//将新的图片名称写入itemsQueryVo
itemsQueryVo.getItemsCustom().setPic(newFileName);
}
//修改ItemsCustom
itemsService.updateItems(id, itemsQueryVo);
//使用转发,转发到商品列表页面
return "forward:queryItems.action";
}