@RequestMapping("update")
public String update(Person per,MultipartFile file,HttpServletRequest request) throws Exception {
String realPath = request.getSession().getServletContext().getRealPath("upload");
String fileName = file.getOriginalFilename();//取文件名
//解决同名问题
fileName = UUID.randomUUID().toString().replace("-", "")+fileName.substring(fileName.lastIndexOf("."));
File f1=new File(realPath,fileName);
if(!f1.exists()){
f1.mkdirs();//如果不存在则创建其目录
}
file.transferTo(f1);//执行上传
per.setFilepath(fileName);//改变一下路径
perdb.updatePerson(per);// 实现修改功能
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="//upload//"+per.getFilepath();//从upload下取图片的路径
FileDownLoad.download(filepath, request, response);
return null;
}
网友评论