<!-- 文件上传 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 设置上传文件的最大尺寸为5MB -->
<property name="maxUploadSize">
<value>5242880</value>
</property>
</bean>
- jar包
CommonsMultipartResolver解析器 commons-fileupload和commons-io,加入jar包
文件上传所需jar包.png
//商品修改提交
@RequestMapping("/editItemSubmit")
public String editItemSubmit(Items items, MultipartFile pictureFile)throws Exception{
//原始文件名
String pictureFile_name = pictureFile.getOriginFilename();
//新文件名称
String newFileName = UUID.randonUUID().toString()+pictureFile_name.substring(pictureFile_name.lastIndexof("."));
//上传图片
File uploadPic = new java.io.File("F:/develop/uplaoad/temp/" + newFileName);
if(!uploadPic.exists()){
uploadPic.mkdirs();
}
//向磁盘写文件
pictureFile.transferTo(uploadPic);
}
- 页面:
form添加enctype="multipart/form-data"
<form id="itemForm"
action="${pageContext.request.contextPath }/item/editItemSubmit.action"
method="post" enctype="multipart/form-data">
<input type="hidden" name="pic" value="${item.pic }" />
<tr>
<td>商品图片</td>
<td><c:if test="${item.pic !=null}">
![](/pic/${item.pic})
<br />
</c:if> <input type="file" name="pictureFile" /></td>
</tr>
网友评论