Springmvc文件上传下载

@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的配置
【Springmvc文件上传下载】

下载
@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; }

    推荐阅读