springmvc 的上传功能

来源:互联网 发布:淘宝水族箱品牌排行榜 编辑:程序博客网 时间:2024/05/20 03:05

springmvc 的上传功能,比较简单。记录下

 @RequestMapping("doUpload")     public String UploadFile(HttpServletRequest request,@RequestParam(value = "file", required = false)     MultipartFile multipartFile){     //获取上传路径      String savePath = request.getSession().getServletContext().getRealPath("upload");        System.out.println("获取文件名 " + multipartFile.getOriginalFilename());        //获取文件名        String filename = multipartFile.getOriginalFilename();        if (StringUtils.isEmpty(filename)){            return  "err.html";        }else{            //获取完整路径名,包含文件名            //  String path = savePath + File.separator + filename;            //创建上传存放文件夹            File upFile = new File(savePath,filename);            //    System.out.println("path:" + path + "  ");            //  File file = new File(savePath);            //判断上传文件是否存在            if (!upFile.exists() && !upFile.isDirectory()){                System.out.println("目录不存在,咱创建个");                // file.mkdir();                upFile.mkdirs();            }            try {            //上传                multipartFile.transferTo(upFile);                upFile.delete();            } catch (IOException e) {                e.printStackTrace();            }            return  "index.html";        }
0 0