需求: 示範Spring-Boot 透過表單註冊用戶,並支援上傳圖片
程式碼實作-檔案上傳
請建立templates/upload.html,確保頭像只能選擇一個,而寵物可上傳多個圖片
rrreee2.建立src\main\java\com\llp\springboot\controller \UploadController.java
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>upload</title> </head> <body bgcolor="#CED3FE"> <img src="images/1.GIF"/ alt="SpringBoot檔案上傳功能如何實現" > <hr/> <div > <h2>注册用户~</h2> <form action="#" th:action="@{/upload}" method="post" enctype="multipart/form-data"> 用户名:<input type="text" name="name"/><br/><br/> 电 邮:<input type="text" name="email"/><br/><br/> 年 龄:<input type="text" name="age"/><br/><br/> 职 位:<input type="text" name="job"/><br/><br/> 头 像:<input type="file" name="header"><br/><br/> 宠 物:<input type="file" name="photos" multiple><br/><br/> <input type="submit" value="注册"/> <input type="reset" value="重新填写"/> </form> </div> <hr/> </body> </html>
@Slf4j @Controller public class UploadController { //处理转发到用户注册-可以完成文件上传页面 @GetMapping("/upload.html") public String uploadPage() { return "upload";// 视图解析,转发到templates/upload.html } @PostMapping("/upload") @ResponseBody public String upload(@RequestParam("name") String name, @RequestParam("email") String email, @RequestParam("age") Integer age, @RequestParam("job") String job, @RequestParam("header") MultipartFile header, @RequestParam("photos") MultipartFile[] photos) throws IOException { log.info("name:{},email:{},age:{},job:{},header.size:{},photos.length:{}",name,email,age,job,header.getSize(),photos.length); //1.获取源文件名称 String originalFilename = header.getOriginalFilename(); // /E:/IdeaProjects/springboot-sysuser/target/classes/ String path = ResourceUtils.getURL("classpath:").getPath(); System.out.println(path); File file = new File(path+"static/images/upload/"); if(!file.exists()){ file.mkdirs(); } header.transferTo(new File(path+"static/images/upload/"+originalFilename)); return "注册用户成功/文件上传成功"; } }
originalFilename = UUID.randomUUID().toString().replaceAll("-","") System.nanoTime() originalFilename;,實作就是重新思路指定一個不重複的檔案名稱
@PostMapping("/upload") @ResponseBody public String upload(@RequestParam("name") String name, @RequestParam("email") String email, @RequestParam("age") Integer age, @RequestParam("job") String job, @RequestParam("header") MultipartFile header, @RequestParam("photos") MultipartFile[] photos) throws IOException { log.info("name:{},email:{},age:{},job:{},header.size:{},photos.length:{}",name,email,age,job,header.getSize(),photos.length); //1.获取源文件名称 String originalFilename = header.getOriginalFilename(); originalFilename = UUID.randomUUID().toString().replaceAll("-","")+System.nanoTime()+originalFilename; //2.获取文件上传的路径 // /E:/IdeaProjects/springboot-sysuser/target/classes/ String path = ResourceUtils.getURL("classpath:").getPath(); System.out.println(path); //3.动态的创建文件上传目录 File file = new File(path+"static/images/upload/"); if(!file.exists()){ file.mkdirs(); } //4.将文件传输到目标目录 header.transferTo(new File(path+"static/images/upload/"+originalFilename)); return "注册用户成功/文件上传成功"; }
public class WebUtils { //定义一个文件上传的路径 public static String UPLOAD_FILE_DIRECTORY = "static/images/upload/"; //编写方法,生成一个目录-根据当前日期 年/月/日 public static String getUploadFileDirectory() { return UPLOAD_FILE_DIRECTORY + new SimpleDateFormat("yyyy/MM/dd").format(new Date()); } }
#
以上是SpringBoot檔案上傳功能如何實現的詳細內容。更多資訊請關注PHP中文網其他相關文章!