1.文件必須選擇app
@PostMapping("/UpLoad")
public void UpLoad(@RequestParam MultipartFile file,HttpServletRequest request) throws IllegalStateException, IOException{ if(!file.isEmpty()){ //上傳文件路徑 String path=""; File fileAllPath=new File(path); //上傳文件名 String fileName=file.getOriginalFilename(); File filePath=new File(path,fileName); //判斷是否存在,不存在新建 if(!filePath.getParentFile().exists()){ filePath.getParentFile().mkdir(); } //將文件放到一個文件目錄中去 file.transferTo(new File(path + File.separator + fileName)); } //參數獲取 request.getParameter("參數對應name"); }
2.文件自由選擇spa
@PostMapping("/UpLoad")
public void UpLoad(HttpServletRequest request) throws IllegalStateException, IOException{ //判斷是否有文件須要上傳 String contentType = request.getContentType(); if (contentType != null && contentType.toLowerCase().startsWith("multipart/")) { MultipartHttpServletRequest multipartRequest = WebUtils.getNativeRequest(request, MultipartHttpServletRequest.class); MultipartFile file = multipartRequest.getFile("file"); //file與傳遞過來的那麼保持一致 } if(!file.isEmpty()){ //上傳文件路徑 String path=""; File fileAllPath=new File(path); //上傳文件名 String fileName=file.getOriginalFilename(); File filePath=new File(path,fileName); //判斷是否存在,不存在新建 if(!filePath.getParentFile().exists()){ filePath.getParentFile().mkdir(); } //將文件放到一個文件目錄中去 file.transferTo(new File(path + File.separator + fileName)); } //參數獲取 request.getParameter("參數對應name"); }