Springboot 上传文件
@PostMapping("/upload")//springboot可以直接扫描resource下的static文件夹下的静态文件 public String upload(@RequestParam("head_img")MultipartFile file){ String name = request.getParameter("name"); log.info(name); String fileName = file.getOriginalFilename(); log.info("上传文件名为:{}",fileName); String suffixName = fileName.substring(fileName.lastIndexOf(".")); log.info("文件后缀名为:{}",suffixName); fileName = UUID.randomUUID()+suffixName; File dest = new File(filePath+fileName); try { file.transferTo(dest); return CodeEnmu.SUCCESS.getMsg(); }catch (Exception e){ e.printStackTrace(); } return CodeEnmu.ERROR.getMsg(); }
<!DOCTYPE html> <html> <head> <title>uploading.html</title> <meta http-equiv="content-type" content="text/html" charset="UTF-8"> </head> <body> <form enctype="multipart/form-data"method="post" action="/upload"> 文件名:<input type="file" name="head_img"> 姓名: <input type="text" name="name"> <input type="submit" value="上传"> </form> </body> </html>