SpringMVC上传图片
1、配置图片上传解析器:SpringMVC中使用 commons-fileupload;(要导入相应 jar包)
2、编写 Controller方法;
(springmvc.xml 文件中配置图片上传解析器;
再在Controller的方法public String editItemsSubmit( MultipartFile picFile )
中添加一个参数( MultipartFile picFile )
来接收
JSP页面传过来的文件<input type="file" name="picFile"/>
,
再写入到文件中picFile.transferTo(file);
,
再设置相应的属性itemsForm.setPic(newFileName);
,
写入到数据库中去做更改itemsService.updateItems(id,itemsCustom);
。)
需求: 在商品修改页面,上传图片;
操作: 用户进入到商品修改页面 — 上传图片 — 点击提交 — 再次进入修改页面,图片在商品页面显示;
(1. editItems.jsp 中添加“商品图片”,以及图片上传域<input type="file" name="picFile"/>
,< form>表单标签中添加 enctype属性:enctype="multipart/form-data"
; 2. springmvc.xml 中配置图片上传解析器; 3 ItemsController.java 中public String editItemsSubmit( MultipartFile picFile )
方法添加参数 (MultipartFile picFile),此参数是 1. editItems.jsp 页面中<input type="file" name="picFile"/>
的 name值保持一致,即接受的是它传过来的值;)
1. editItems.jsp
<%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<body>
<form action="${pageContext.request.contextPath}/items/editItemsSubmit.action" method="post" enctype="multipart/form-data">
<input type="hidden" name="id" value="${item.id}"/>
<table>
<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>
<c:if test="${itemsCustom.pic!=null}">
<img src="images/${itemsCustom.pic}" width="100" height="100"/><br/>
</c:if>
<input type="file" name="picFile"/>
</tr>
<tr><td>订购日期:</td>
<td><input type="text" name="price" value="<fmt:formatDate value='${itemsCustom.createtime}' pattern="yyyy-MM-dd"/>"></td></tr>
<tr><td>商品描述:</td>
<td><input type="text" name="detail" value="${itemsCustom.detail}"></td></tr>
<tr><td colspan="2" align="center"><input type="submit" value="提交"/></td></tr>
</table>
</form>
</body>
2. springmvc.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- (开启spring注解扫描)3.注解开发的Handler -->
<context:component-scan base-package="com.asd"></context:component-scan>
<!-- 1.注解映射器 -->
<bean name="" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingInfoHandlerMapping">
</bean>
<!-- 2.注解适配器 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean>
<!-- 4.视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 5.文件上传解析器-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize">//设置上传文件最大尺寸为5MB
<value>5242880</value>
</property>
</bean>
</beans>
3. ItemsController.java
@Controller
@RequestMapping("/items")
public class ItemsController{
@Autowired
private ItemsService itemsService;
@RequestMapping("/editItemsSubmit")
public String editItemsSubmit(Model model,Integer id,HttpServlerRequest request,
@ModelAttribute(value="itemsCustom") ItemsCustom itemsCustom,
MultipartFile picFile) throws Exception{
model.addAttribute("id",id);
if (picFile!=null&&picFile.getOriginalFilename()!=null&&picFile.getOriginalFilename().length()>0) {
//进行文件上传
String fielPath=request.getRealPath("/");
//上传的原始文件名
String oldFileName=picFile.getOriginalFilename();
//新文件名
String newFileName=UUID.randomUUID()+oldFileName.subString(oldFileName.lastIndexOf("."));
//新文件
File file=new File(fielPath+"images/"+newFileName);
//写入文件
picFile.transferTo(file);
//新图片写入到数据库中
itemsForm.setPic(newFileName);
}
itemsService.updateItems(id,itemsCustom);
return "redirect:queryItems.action";
}
}