在tomcat上配置图片虚拟目录,在tomcat下conf/server.xml中添加:
<Context docBase="D:developupload emp" path="/pic" reloadable="false"/>
访问
http://localhost:8080/pic即可访问D:developupload emp下的图片。
也可以通过eclipse配置,如下图
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize">
<value>2097152value>
property>
<property name="defaultEncoding">
<value>UTF-8value>
property>
bean>
- 控制
导入jar包:
commons-fileupload-1.2.2.jar
commons-io-2.4.jar
@RequestMapping(value = "/updateitem.action")
public String updateitem(QueryVo vo,MultipartFile pictureFile) throws Exception{
String name = UUID.randomUUID().toString().replaceAll("-", "");
String ext = FilenameUtils.getExtension(pictureFile.getOriginalFilename());
pictureFile.transferTo(new File("D:\upload\" + name + "." + ext));
vo.getItems().setPic(name + "." + ext);
itemService.updateItemsById(vo.getItems());
return "redirect:/itemEdit.action?id=" + vo.getItems().getId();
}