1、SpringBoot采用FileUpload組件實現上傳處理,在控制器中可以使用MultipartFile類進行接收。
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 5 https://maven.apache.org/xsd/maven-4.0.0.xsd"> 6 <modelVersion>4.0.0</modelVersion> 7 <parent> 8 <groupId>org.springframework.boot</groupId> 9 <artifactId>spring-boot-starter-parent</artifactId> 10 <version>2.3.5.RELEASE</version> 11 <relativePath /> <!-- lookup parent from repository --> 12 </parent> 13 <groupId>com.example</groupId> 14 <artifactId>demo</artifactId> 15 <version>0.0.1-SNAPSHOT</version> 16 <name>demo</name> 17 <description>Demo project for Spring Boot</description> 18 19 <properties> 20 <java.version>1.8</java.version> 21 <maven-jar-plugin.version>3.1.1</maven-jar-plugin.version> 22 </properties> 23 24 <dependencies> 25 <dependency> 26 <groupId>org.springframework.boot</groupId> 27 <artifactId>spring-boot-starter-web</artifactId> 28 </dependency> 29 30 <dependency> 31 <groupId>org.springframework.boot</groupId> 32 <artifactId>spring-boot-starter-test</artifactId> 33 <scope>test</scope> 34 <exclusions> 35 <exclusion> 36 <groupId>org.junit.vintage</groupId> 37 <artifactId>junit-vintage-engine</artifactId> 38 </exclusion> 39 </exclusions> 40 </dependency> 41 <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-validator --> 42 <dependency> 43 <groupId>org.hibernate</groupId> 44 <artifactId>hibernate-validator</artifactId> 45 <version>6.1.0.Final</version> 46 </dependency> 47 <dependency> 48 <groupId>org.springframework.boot</groupId> 49 <artifactId>spring-boot-starter-thymeleaf</artifactId> 50 </dependency> 51 </dependencies> 52 53 <build> 54 <plugins> 55 <plugin> 56 <groupId>org.springframework.boot</groupId> 57 <artifactId>spring-boot-maven-plugin</artifactId> 58 </plugin> 59 </plugins> 60 <resources> 61 <resource> 62 <directory>src/main/resources</directory> 63 <includes> 64 <include>**/*.properties</include> 65 <include>**/*.yml</include> 66 <include>**/*.xml</include> 67 <include>**/*.p12</include> 68 <include>**/*.html</include> 69 <include>**/*.jpg</include> 70 <include>**/*.png</include> 71 </includes> 72 </resource> 73 </resources> 74 </build> 75 76 </project>
創建控制器類UploadController,利用MultipartFile將上傳文件保存在本地磁盤。
1 package com.demo.controller; 2 3 import java.io.File; 4 import java.io.IOException; 5 import java.util.HashMap; 6 import java.util.Map; 7 import java.util.UUID; 8 9 import org.springframework.stereotype.Controller; 10 import org.springframework.web.bind.annotation.RequestMapping; 11 import org.springframework.web.bind.annotation.RequestMethod; 12 import org.springframework.web.bind.annotation.RequestParam; 13 import org.springframework.web.bind.annotation.ResponseBody; 14 import org.springframework.web.context.request.RequestAttributes; 15 import org.springframework.web.context.request.RequestContextHolder; 16 import org.springframework.web.context.request.ServletRequestAttributes; 17 import org.springframework.web.multipart.MultipartFile; 18 19 /** 20 * 21 * @author 22 * 23 */ 24 @Controller 25 public class UploadController { 26 27 /** 28 * 首先根據此方法跳轉到upload.html界面 29 * 30 * @return 31 */ 32 @RequestMapping(value = "/upload_pre", method = RequestMethod.GET) 33 public String uploadPre() { 34 return "upload"; 35 } 36 37 /** 38 * 文件上傳 39 * 40 * @param name 41 * @param multipartFile 42 * @return 43 * @throws IllegalStateException 44 * @throws IOException 45 */ 46 @RequestMapping(value = "/upload", method = RequestMethod.POST) 47 @ResponseBody 48 public Object upload(String name, @RequestParam(value = "multipartFile") MultipartFile multipartFile) 49 throws IllegalStateException, IOException { 50 Map<String, Object> map = new HashMap<String, Object>(); 51 if (multipartFile != null) { 52 // 設置文件名稱 53 map.put("nameParam", name); 54 // 設置文件名稱 55 map.put("fileame", multipartFile.getName()); 56 // 設置文件類型 57 map.put("contentType", multipartFile.getContentType()); 58 // 設置文件大小 59 map.put("fileSize", multipartFile.getSize()); 60 // 創建文件名稱 61 String fileName = UUID.randomUUID() + "." 62 + multipartFile.getContentType().substring(multipartFile.getContentType().lastIndexOf("/") + 1); 63 // 獲取到文件的路徑信息 64 RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes(); 65 ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) requestAttributes; 66 String filePath = servletRequestAttributes.getRequest().getServletContext().getRealPath("/") + fileName; 67 // 打印保存路徑 68 System.out.println(filePath); 69 // 保存文件的路徑信息 70 map.put("filePath", filePath); 71 // 創建文件 72 File saveFile = new File(filePath); 73 // 文件保存 74 multipartFile.transferTo(saveFile); 75 // 返回信息 76 return map; 77 } else { 78 return "no file "; 79 } 80 } 81 82 }
在src/main/resources/templates下面創建upload.html文件。
1 <!DOCTYPE html> 2 <html xmlns="http://www.thymeleaf.org"></html> 3 <head> 4 <meta charset="UTF-8"> 5 <title>搞事情啊!!!</title> 6 </head> 7 <body> 8 <h1>搞事情啊!!!</h1> 9 10 <form th:action="@{/upload}" method="post" enctype="multipart/form-data"> 11 姓名:<input type="text" name="name"/><br/> 12 <!-- 切記,這里的name="multipartFile"必須和控制層的參數一致性MultipartFile multipartFile --> 13 照片:<input type="file" name="multipartFile"/><br/> 14 <input type="submit" value="上傳"/><br/> 15 </form> 16 17 </body> 18 </html>
控制層類UploadController接收到此請求信息后,如果有上傳文件存在,則會直接返回上傳信息(開發者也可以根據情況選擇將文件保存)。
演示效果,如下所示:
2、Springboot上傳文件限制,在實際項目開發中,需要對用戶上傳文件的大小進行限制,這樣才可以保證服務器的資源不被浪費。修改application.properties配置文件,如下所示:
1 # 啟用http上傳 2 spring.servlet.multipart.enabled=true 3 # 設置支持的單個上傳文件的大小限制 4 spring.servlet.multipart.max-file-size=10MB 5 # 設置最大的請求文件大小,設置總體大小請求 6 spring.servlet.multipart.max-request-size=20MB 7 # 當上傳文件達到指定配置量的時候,將文件內容寫入磁盤 8 spring.servlet.multipart.file-size-threshold=512KB 9 # 設置上傳的臨時目錄 10 spring.servlet.multipart.location=/
對於Springboot上傳文件的上傳限制,也可以利用Bean實現同樣的效果,實現代碼,如下所示:
1 package com.demo.config; 2 3 import javax.servlet.MultipartConfigElement; 4 5 import org.springframework.boot.web.servlet.MultipartConfigFactory; 6 import org.springframework.context.annotation.Bean; 7 import org.springframework.context.annotation.Configuration; 8 import org.springframework.util.unit.DataSize; 9 10 @Configuration 11 public class UploadConfig { 12 13 @Bean 14 public MultipartConfigElement getMultipartConfig() { 15 MultipartConfigFactory config = new MultipartConfigFactory(); 16 // 設置上傳文件的單個大小限制 17 config.setMaxRequestSize(DataSize.parse("10MB")); 18 // 設置總的上傳的大小限制 19 config.setMaxRequestSize(DataSize.parse("100MB")); 20 // 設置臨時保存目錄 21 config.setLocation("/"); 22 // 創建一個上傳配置並返回 23 return config.createMultipartConfig(); 24 } 25 26 }
3、Springboot上傳多個文件如果要進行多個文件的上傳,需要通過MultipartHttpServletRequest進行文件接收。首先修改upload.html頁面,定義多個文件上傳控件。
1 <!DOCTYPE html> 2 <html xmlns="http://www.thymeleaf.org"></html> 3 <head> 4 <meta charset="UTF-8"> 5 <title>搞事情啊!!!</title> 6 </head> 7 <body> 8 <h1>搞事情啊!!!</h1> 9 10 <form th:action="@{/upload}" method="post" enctype="multipart/form-data"> 11 姓名:<input type="text" name="name"/><br/> 12 <!-- 切記,這里的name="multipartFile"必須和控制層的參數一致性MultipartFile multipartFile --> 13 文件一:<input type="file" name="multipartFile"/><br/> 14 文件二:<input type="file" name="multipartFile"/><br/> 15 文件三:<input type="file" name="multipartFile"/><br/> 16 <input type="submit" value="上傳"/><br/> 17 </form> 18 19 </body> 20 </html>
修改UploadController.java控制器,實現多個文件上傳,此時可以根據接受參數的不同,方法可以有不同的實現 ,如果接受參數是這樣的,@RequestParam(value = "multipartFile") MultipartFile[] multipartFile,實現代碼,如下所示:
1 package com.demo.controller; 2 3 import java.io.File; 4 import java.io.IOException; 5 import java.util.HashMap; 6 import java.util.Map; 7 import java.util.UUID; 8 9 import org.springframework.stereotype.Controller; 10 import org.springframework.web.bind.annotation.RequestMapping; 11 import org.springframework.web.bind.annotation.RequestMethod; 12 import org.springframework.web.bind.annotation.RequestParam; 13 import org.springframework.web.bind.annotation.ResponseBody; 14 import org.springframework.web.context.request.RequestAttributes; 15 import org.springframework.web.context.request.RequestContextHolder; 16 import org.springframework.web.context.request.ServletRequestAttributes; 17 import org.springframework.web.multipart.MultipartFile; 18 19 /** 20 * 21 * @author 22 * 23 */ 24 @Controller 25 public class UploadController { 26 27 /** 28 * 首先根據此方法跳轉到upload.html界面 29 * 30 * @return 31 */ 32 @RequestMapping(value = "/upload_pre", method = RequestMethod.GET) 33 public String uploadPre() { 34 return "upload"; 35 } 36 37 /** 38 * 文件上傳 39 * 40 * @param name 41 * @param multipartFile 42 * @return 43 * @throws IllegalStateException 44 * @throws IOException 45 */ 46 @RequestMapping(value = "/upload", method = RequestMethod.POST) 47 @ResponseBody 48 public Object upload(String name, @RequestParam(value = "multipartFile") MultipartFile[] multipartFile) 49 throws IllegalStateException, IOException { 50 Map<String, Object> map = new HashMap<String, Object>(); 51 if (multipartFile != null && multipartFile.length > 0) { 52 for (int i = 0; i < multipartFile.length; i++) { 53 // 設置文件名稱 54 map.put("nameParam", name); 55 // 設置文件名稱 56 map.put("fileame", multipartFile[i].getName()); 57 // 設置文件類型 58 map.put("contentType", multipartFile[i].getContentType()); 59 // 設置文件大小 60 map.put("fileSize", multipartFile[i].getSize()); 61 // 創建文件名稱 62 String fileName = UUID.randomUUID() + "." + multipartFile[i].getContentType() 63 .substring(multipartFile[i].getContentType().lastIndexOf("/") + 1); 64 // 獲取到文件的路徑信息 65 RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes(); 66 ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) requestAttributes; 67 String filePath = servletRequestAttributes.getRequest().getServletContext().getRealPath("/") + fileName; 68 // 打印保存路徑 69 System.out.println(filePath); 70 // 保存文件的路徑信息 71 map.put("filePath", filePath); 72 // 創建文件 73 File saveFile = new File(filePath); 74 // 文件保存 75 multipartFile[i].transferTo(saveFile); 76 // 返回信息 77 } 78 return map; 79 } else { 80 return "no file "; 81 } 82 } 83 84 }
如果接受參數是HttpServletRequest request,代碼是這樣的,明顯沒有第一種方便,如下所示:
1 package com.demo.controller; 2 3 import java.io.File; 4 import java.io.IOException; 5 import java.util.ArrayList; 6 import java.util.HashMap; 7 import java.util.Iterator; 8 import java.util.List; 9 import java.util.Map; 10 import java.util.UUID; 11 12 import javax.servlet.http.HttpServletRequest; 13 14 import org.springframework.stereotype.Controller; 15 import org.springframework.web.bind.annotation.RequestMapping; 16 import org.springframework.web.bind.annotation.RequestMethod; 17 import org.springframework.web.bind.annotation.RequestParam; 18 import org.springframework.web.bind.annotation.ResponseBody; 19 import org.springframework.web.context.request.RequestAttributes; 20 import org.springframework.web.context.request.RequestContextHolder; 21 import org.springframework.web.context.request.ServletRequestAttributes; 22 import org.springframework.web.multipart.MultipartFile; 23 import org.springframework.web.multipart.MultipartHttpServletRequest; 24 25 /** 26 * 27 * @author 28 * 29 */ 30 @Controller 31 public class UploadController { 32 33 /** 34 * 首先根據此方法跳轉到upload.html界面 35 * 36 * @return 37 */ 38 @RequestMapping(value = "/upload_pre", method = RequestMethod.GET) 39 public String uploadPre() { 40 return "upload"; 41 } 42 43 /** 44 * 文件上傳 45 * 46 * @param name 47 * @param multipartFile 48 * @return 49 * @throws IllegalStateException 50 * @throws IOException 51 */ 52 @RequestMapping(value = "/upload", method = RequestMethod.POST) 53 @ResponseBody 54 public Object upload2(String name, HttpServletRequest request) throws IllegalStateException, IOException { 55 List<String> result = new ArrayList<String>(); 56 if (request instanceof MultipartHttpServletRequest) { 57 MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) request; 58 List<MultipartFile> files = multipartHttpServletRequest.getFiles("multipartFile"); 59 Iterator<MultipartFile> iterator = files.iterator(); 60 while (iterator.hasNext()) { 61 // 取出每一個文件 62 MultipartFile file = iterator.next(); 63 try { 64 // 保存上傳信息 65 result.add(this.saveFile(file)); 66 } catch (Exception e) { 67 e.printStackTrace(); 68 } 69 } 70 } 71 return result; 72 } 73 74 /** 75 * 文件保存處理 76 * 77 * @param file 78 * 上傳文件 79 * @return 文件保存路徑 80 * @throws Exception 81 * 上傳異常 82 */ 83 public String saveFile(MultipartFile multipartFile) throws Exception { 84 String filePath = "no file!!!"; 85 if (multipartFile != null && multipartFile.getSize() > 0) { 86 // 創建文件名稱 87 String fileName = UUID.randomUUID() + "." 88 + multipartFile.getContentType().substring(multipartFile.getContentType().lastIndexOf("/") + 1); 89 // 獲取到文件的路徑信息 90 RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes(); 91 ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) requestAttributes; 92 filePath = servletRequestAttributes.getRequest().getServletContext().getRealPath("/") + fileName; 93 // 打印保存路徑 94 System.out.println(filePath); 95 File saveFile = new File(filePath); 96 // 文件保存 97 multipartFile.transferTo(saveFile); 98 } 99 return filePath; 100 } 101 102 }