1springmvc 的上传功能,比较简单。记录下 2 3``` 4 @RequestMapping("doUpload") 5 public String UploadFile(HttpServletRequest request, 6 @RequestParam(value = "file", required = false)MultipartFile multipartFile){ 7 //获取上传路径 8 String savePath = request.getSession().getServletContext().getRealPath("upload"); 9 System.out.println("获取文件名 " + multipartFile.getOriginalFilename()); 10 //获取文件名 11 String filename = multipartFile.getOriginalFilename(); 12 if (StringUtils.isEmpty(filename)){ 13 return "err.html"; 14 }else{ 15 //获取完整路径名,包含文件名 16 // String path = savePath + File.separator + filename; 17 //创建上传存放文件夹 18 File upFile = new File(savePath,filename); 19 // System.out.println("path:" + path + " "); 20 // File file = new File(savePath); 21 //判断上传文件是否存在 22 if (!upFile.exists() && !upFile.isDirectory()){ 23 System.out.println("目录不存在,咱创建个"); 24 // file.mkdir(); 25 upFile.mkdirs(); 26 } 27 try { 28 //上传 29 multipartFile.transferTo(upFile); 30 upFile.delete(); 31 } catch (IOException e) { 32 e.printStackTrace(); 33 } 34 return "index.html"; 35 }
springmvc的上传功能
Easter79
2021-10-12
1053 1 0
点赞
收藏
评论区
加载中...