需求:商品列表中上传图片
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;
}
}
效果: