MultipartFile 文件上传

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