当前位置:
首页
文章
前端
详情

springmvc的上传功能

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";
        }

免责申明:本站发布的内容(图片、视频和文字)以转载和分享为主,文章观点不代表本站立场,如涉及侵权请联系站长邮箱:xbc-online@qq.com进行反馈,一经查实,将立刻删除涉嫌侵权内容。