@RequestMapping("update")
public String update(Person per,MultipartFile file,HttpServletRequest request) throws Exception {
String realPath = request.getSession().getServletContext().getRealPath("upload");
String fileName = file.getOriginalFilename();//取文件名
//解決同名問(wèn)題
fileName = UUID.randomUUID().toString().replace("-", "")+fileName.substring(fileName.lastIndexOf("."));
File f1=new File(realPath,fileName);
if(!f1.exists()){
f1.mkdirs();//如果不存在則創(chuàng)建其目錄
}
file.transferTo(f1);//執(zhí)行上傳
per.setFilepath(fileName);//改變一下路徑
perdb.updatePerson(per);// 實(shí)現(xiàn)修改功能
return "redirect:list.do";
}
springmvc.xml的配置
<!-- 配置視圖解析器 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 前綴 -->
<property name="prefix" value="/WEB-INF/per/"></property>
<!-- 后綴 -->
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 配置spring mvc上傳圖片大小,multipartResolver名不能改 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="104857600"></property>
</bean>
下載
@RequestMapping("download")
public String download(HttpServletRequest request,HttpServletResponse response,Person per) throws Exception{//下載
String filepath="http://upload//"+per.getFilepath();//從upload下取圖片的路徑
FileDownLoad.download(filepath, request, response);
return null;
}