首页 > 学院 > 开发设计 > 正文

SpringMVC实现图片上传

2019-11-06 07:44:56
字体:
来源:转载
供稿:网友

1上传图片

1.1  配置虚拟目录

在tomcat上配置图片虚拟目录,在tomcat下conf/server.xml中添加: <Context docBase="E:/temp" path="/pic" reloadable="false"/>

访问http://localhost:8080/pic即可访问E:/temp下的图片。

1.2 配置解析器

<!--文件上传 -->

    <beanid="multipartResolver"

       class="org.sPRingframework.web.multipart.commons.CommonsMultipartResolver">

       <!-- 设置上传文件的最大尺寸为5MB -->

       <property name="maxUploadSize">

           <value>5242880</value>

       </property>

    </bean>

1.2添加 jar包

CommonsMultipartResolver解析器依赖commons-fileupload和commons-io,加入如下jar包:

1.3  图片上传

controller部分代码编写
@RequestMapping("/editItemsSubmit") 	public String  editItemsSubmit(Model model,Integer id, @Validated ItemsCustom itemsCustom,BindingResult bindingResult,			MultipartFile items_pic//接收图片			) throws Exception  { 				System.out.println(id);				List<ObjectError> allError=  bindingResult.getAllErrors();		for (ObjectError objectError : allError) {			System.out.println(objectError.getDefaultMessage());		}		model.addAttribute("allError", allError);				//		String originalFileName= items_pic.getOriginalFilename();		if (items_pic!=null && originalFileName!=null && originalFileName.length()>0) {			String pic_path="E://temp//";			String newFileName = UUID.randomUUID()+originalFileName.substring(originalFileName.lastIndexOf("."));			File newFile  = new File(pic_path+ newFileName);			items_pic.transferTo(newFile);//存入磁盘						itemsCustom.setPic(newFileName); 		//新图片写到itemsCustom中					}		itemsService.updateItems(id, itemsCustom);		//return "success";		//return "items/editItems";			//return "forward:queryItems.action";		//return "redirect:queryItems.action";		return "redirect:queryItems.action";	}1.4页面部分设置

form添加enctype="multipart/form-data":

<form id="itemForm" action="${pageContext.request.contextPath}/items/editItemsSubmit.action" method="post"   	 	enctype="multipart/form-data">      	 			<!--  enctype="multipart/form-data" post 二进制信息  -->   	 			<input type="hidden" name="pic" value="${itemsCustom.pic}">   			<input type="hidden" name="id" value="${itemsCustom.id}">   			修改商品信息:   			<table width="100%" border="1">    				<tr>   					<td>商品名称</td>    					<td><input type="text"  name="name" value="${itemsCustom.name}"/></td>   				</tr>   				<tr>   					<td>商品价格</td>   					 <td><input type="text"  name="price" value="${itemsCustom.price}"/></td>   				</tr>   				<tr>   					<td>生产日期</td>    					<td><input type="text"  name="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="detail">${itemsCustom.detail }</textarea>					</td>				</tr>				<tr>					<td colspan="2" align="center"><input type="submit" value="提交"/>					</td>				</tr>   			</table>   		</form>

file的name与controller形参一致:

 <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>

上一篇:Maven介绍

下一篇:后缀数组(修订版)

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表