springMVC图片上传
xml配置
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize"><value>1000000</value></property>
</bean>
@RequestMapping("upload.do")
public ModelAndView upload(MultipartFile photo) throws IllegalStateException, IOException {
String oldName = photo.getOriginalFilename();
System.out.println("oldName:"+oldName);
String suffix = oldName.substring(oldName.indexOf("."),oldName.length());
System.out.println("suffix:"+suffix);
String newName = UUID.randomUUID().toString()+suffix;
File file = new File("E:\\images\\",newName);
photo.transferTo(file);
ModelAndView mav=new ModelAndView();
mav.addObject("path","/photo/"+newName);
mav.setViewName("upload");
return mav;
}
上传页面
<form action="<%=request.getContextPath() %>/upload.do" method="post" enctype="multipart/form-data">
file:<input type="file" name="photo"/><br/>
<input type="submit" name="add"/>
</form>
<img alt="" src="http://localhost:7070${path }">
还没有评论,来说两句吧...