1. 程式人生 > >SpringMVC框架(2)之(2.1 使用 SpringMVC上傳圖片)

SpringMVC框架(2)之(2.1 使用 SpringMVC上傳圖片)

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.javapublic 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"; 
	}
}