需求:商品清單中上傳圖檔
1 SpringMVC實作圖檔上傳需要加入的jar包,如下圖:

2 在tomcat上配置圖檔虛拟目錄,在tomcat下conf/server.xml中添加:
< Context docBase="…temp(圖檔路徑)" path="/pic" reloadable=“false”/>
通路http://localhost:8080/pic即可通路temp下的圖檔。
放在 < host> 節點中 如下圖
3 springmvc.xml中配置上傳解析器
<!-- 檔案上傳,id必須設定為multipartResolver -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 設定檔案上傳大小 -->
<property name="maxUploadSize" value="5000000" />
</bean>
4 JSP頁面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>修改商品資訊</title>
</head>
<body>
<!-- 上傳圖檔是需要指定屬性 enctype="multipart/form-data" -->
<!-- <form id="itemForm" action="" method="post" enctype="multipart/form-data"> -->
<form id="itemForm" action="${pageContext.request.contextPath }/updateitem.action" method="post" enctype="multipart/form-data">
<input type="hidden" name="id" value="${item.id }" /> 修改商品資訊:
<table width="100%" border=1>
<tr>
<td>商品名稱</td>
<td><input type="text" name="name" value="${item.name }" /></td>
</tr>
<tr>
<td>商品價格</td>
<td><input type="text" name="price" value="${item.price }" /></td>
</tr>
<tr>
<td>商品生産日期</td>
<td><input type="text" name="createtime"
value="<fmt:formatDate value="${item.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/>" /></td>
</tr>
<tr>
<td>商品圖檔</td>
<td>
<c:if test="${item.pic !=null}">
<img src="${item.pic}" width=100 height=100/>
<br/>
</c:if>
<input type="file" name="pictureFile"/>
</td>
</tr>
<tr>
<td>商品簡介</td>
<td><textarea rows="3" cols="30" name="detail">${item.detail }</textarea>
</td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="送出" />
</td>
</tr>
</table>
</form>
</body>
</html>
5 ItemController圖檔上傳邏輯代碼:
package com.sea.ssm.controller;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import com.sea.ssm.pojo.Item;
import com.sea.ssm.service.ItemService;
import com.sea.ssm.util.pojo.QueryVo;
@Controller
public class ItemController {
@Value("G:\\SeaTemp\\")
private String PIC_PATH;
@Autowired
private ItemService itemService;
@RequestMapping("/itemList")
public ModelAndView itemList() {
List<Item> list = itemService.queryItemList();
ModelAndView modelAndView = new ModelAndView("itemList");
modelAndView.addObject("itemList", list);
return modelAndView;
}
@RequestMapping("/itemEdit.action")
public ModelAndView itemEdit(int id) {
Item item = itemService.queryById(id);
ModelAndView modelAndView = new ModelAndView("editItem");
modelAndView.addObject("item", item);
return modelAndView;
}
@RequestMapping("/updateitem.action")
public void updateItem(HttpServletResponse response, Item item,MultipartFile pictureFile) throws IOException {
System.out.println(item);
//使用UUID給檔案取一個實體名稱
String fileName=UUID.randomUUID().toString();
//擷取檔案字尾
String fname = pictureFile.getOriginalFilename();
String ext = fname.substring(fname.lastIndexOf("."));
//将圖檔傳入指定目錄
pictureFile.transferTo(new File(PIC_PATH+fileName+ext));
//将圖檔的通路路徑存儲到item對象中
item.setPic("/pic/"+fileName+ext);
itemService.updateItem(item);
response.sendRedirect("itemList.action");
}
@RequestMapping("/queryItem")
public ModelAndView queryVo(QueryVo vo) {
System.out.println(vo.getItem().getId());
System.out.println(vo.getItem().getName());
List<Item> itemList = itemService.queryByVo(vo);
ModelAndView modelAndView = new ModelAndView("itemList");
modelAndView.addObject("itemList", itemList);
return modelAndView;
}
}
效果: