本文源碼 GitHub地址:知了一笑 https://github.com/cicadasmile/spring-boot-base
文件上傳是項目開發中一個很經常使用的功能,常見的如頭像上傳,各種文檔數據上傳等。SpringBoot使用MultiPartFile接收來自表單的file文件,而後執行上傳文件。該案例基於SpringBoot2.0中yml配置,管理文件上傳的常見屬性。該案例演示單文件上傳和多文件上傳。html
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <body> <hr/> <h3>一、單文件上傳</h3> <form method="POST" action="/upload1" enctype="multipart/form-data"> 上傳人:<input type="text" name="userName" /><br/> 文件一:<input type="file" name="file" /><br/> <input type="submit" value="Submit" /> </form> <hr/> <h3>二、多文件上傳</h3> <form method="POST" action="/upload2" enctype="multipart/form-data"> 上傳人:<input type="text" name="userName" /><br/> 文件一:<input type="file" name="file" /><br/> 文件二:<input type="file" name="file" /><br/><br/> <input type="submit" value="Submit" /> </form> </body> </html> <hr/>
import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; @Controller public class PageController { /** * 上傳頁面 */ @GetMapping("/uploadPage") public String uploadPage (){ return "upload" ; } }
上傳文件單個限制
max-file-size: 5MB
上傳文件總大小限制
max-request-size: 6MBjava
spring: application: # 應用名稱 name: node14-boot-file servlet: multipart: # 啓用 enabled: true # 上傳文件單個限制 max-file-size: 5MB # 總限制 max-request-size: 6MB
若是單個文件大小超出1MB,拋出異常
FileSizeLimitExceededException:
若是上傳文件總大小超過6MB,拋出異常
SizeLimitExceededException:
這樣就徹底驗證了YML文件中的配置,有效且正確。node
import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletRequest; import java.io.File; import java.util.Map; @RestController public class FileController { private static final Logger LOGGER = LoggerFactory.getLogger(FileController.class) ; /** * 測試單個文件上傳 */ @RequestMapping("/upload1") public String upload1 (HttpServletRequest request, @RequestParam("file") MultipartFile file){ Map<String, String[]> paramMap = request.getParameterMap() ; if (!paramMap.isEmpty()){ LOGGER.info("paramMap == >>{}",paramMap); } try{ if (!file.isEmpty()){ // 打印文件基礎信息 LOGGER.info("Name == >>{}",file.getName()); LOGGER.info("OriginalFilename == >>{}",file.getOriginalFilename()); LOGGER.info("ContentType == >>{}",file.getContentType()); LOGGER.info("Size == >>{}",file.getSize()); // 文件輸出地址 String filePath = "F:/boot-file/" ; new File(filePath).mkdirs(); File writeFile = new File(filePath, file.getOriginalFilename()); file.transferTo(writeFile); } return "success" ; } catch (Exception e){ e.printStackTrace(); return "系統異常" ; } } /** * 測試多文件上傳 */ @RequestMapping("/upload2") public String upload2 (HttpServletRequest request, @RequestParam("file") MultipartFile[] fileList){ Map<String, String[]> paramMap = request.getParameterMap() ; if (!paramMap.isEmpty()){ LOGGER.info("paramMap == >>{}",paramMap); } try{ if (fileList.length > 0){ for (MultipartFile file:fileList){ // 打印文件基礎信息 LOGGER.info("Name == >>{}",file.getName()); LOGGER.info("OriginalFilename == >>{}",file.getOriginalFilename()); LOGGER.info("ContentType == >>{}",file.getContentType()); LOGGER.info("Size == >>{}",file.getSize()); // 文件輸出地址 String filePath = "F:/boot-file/" ; new File(filePath).mkdirs(); File writeFile = new File(filePath, file.getOriginalFilename()); file.transferTo(writeFile); } } return "success" ; } catch (Exception e){ return "fail" ; } } }
GitHub地址:知了一笑 https://github.com/cicadasmile/spring-boot-base 碼雲地址:知了一笑 https://gitee.com/cicadasmile/spring-boot-base