SpringMVC(六)SpringMVC的图片上传
2019-04-15 14:40发布
生成海报
- 配置虚拟目录
在tomcat上配置图片虚拟目录,在tomcat下conf/server.xml中添加:访问http://localhost:8080/pic即可访问F:developupload emp下的图片。也可以通过eclipse配置:- jar包
CommonsMultipartResolver解析器依赖commons-fileupload和commons-io,加入如下jar包:- 配置解析器
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize"> <value>5242880value> property> bean>- 图片上传
//商品修改提交 @RequestMapping("/editItemSubmit") public String editItemSubmit(Items items, MultipartFile pictureFile)throws Exception{ //原始文件名称 String pictureFile_name = pictureFile.getOriginalFilename(); //新文件名称 String newFileName = UUID.randomUUID().toString()+pictureFile_name.substring(pictureFile_name.lastIndexOf(".")); //上传图片 File uploadPic = new java.io.File("F:/develop/upload/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 }" />注意:file的name与controller形参一致:<tr> <td>商品图片td> <td><c:if test="${item.pic !=null}"> <img src="/pic/${item.pic}" width=100 height=100 /> <br /> c:if> <input type="file" name="pictureFile" />td>tr>
打开微信“扫一扫”,打开网页后点击屏幕右上角分享按钮