码迷,mamicode.com
首页 > Web开发 > 详细

文件的上传和下载

时间:2018-04-23 14:23:48      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:tee   nal   image   let   下载   springmvc   exist   str   load   

SpringMVC实现单文件上传

@RequestMapping("/uploadFile")
    public String upload(@RequestParam(value="file") MultipartFile file,
            HttpServletRequest request) throws IllegalStateException, IOException{
        //如果文件不为空,写入上传路径
        if(!file.isEmpty()) {
            //上传文件绝对路径
            String path = request.getServletContext().getRealPath("/images/");
            //上传文件名
            String filename = file.getOriginalFilename();
            File filepath = new File(path,filename);
            //判断路径是否存在,如果不存在就创建一个
            if (!filepath.getParentFile().exists()) { 
                filepath.getParentFile().mkdirs();
            }
            //将上传文件保存到一个目标文件当中
            file.transferTo(new File(path + File.separator + filename));
            String showPath = request.getContextPath()+"/images/"+filename;
            
            request.setAttribute("showPath", showPath);
            return "hello";
        } else {
            return "error";
        }
                
                
        
    }

SpringMVC单文件下载

 

文件的上传和下载

标签:tee   nal   image   let   下载   springmvc   exist   str   load   

原文地址:https://www.cnblogs.com/strugglecola/p/8918756.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!