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