1. 程式人生 > 其它 >SpringBoot - 實現檔案上傳2(多檔案上傳、常用上傳引數配置)

SpringBoot - 實現檔案上傳2(多檔案上傳、常用上傳引數配置)

在前文中我介紹了Spring Boot專案如何實現單檔案上傳,而多檔案上傳邏輯和單檔案上傳基本一致,下面通過樣例進行演示。

二、多檔案上傳

1,程式碼編寫

(1)首先在static目錄中建立一個uploads.html檔案,內容如下:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form action="/uploads" method
="post" enctype="multipart/form-data"> <input type="file" name="uploadFiles" value="請選擇檔案" multiple> <input type="submit" value="上傳"> </form> </body> </html>

(2)接著建立檔案上傳處理介面FileUploadController.java,裡面核心邏輯和單檔案上傳是一樣的,只是多了一個遍歷的步驟,內容如下:

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletRequest; import java.io.File; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.UUID; @RestController
public class FileUploadController { SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd/"); @PostMapping("/uploads") public String upload(MultipartFile[] uploadFiles, HttpServletRequest req) { String result = ""; // 遍歷所有上傳的檔案 for (MultipartFile uploadFile : uploadFiles) { // 上傳的檔案將儲存在專案執行目錄下的 uploadFile 資料夾 String realPath = req.getSession().getServletContext().getRealPath("/uploadFile/"); System.out.println(realPath); // 並且在 uploadFile 資料夾中通過日期對上傳的檔案歸類儲存 // 比如:/uploadFile/2019/06/06/32091e5f-c9e9-4506-9567-43e724f1fe37.png String format = sdf.format(new Date()); File folder = new File(realPath + format); if (!folder.isDirectory()) { folder.mkdirs(); } // 對上傳的檔案重新命名,避免檔案重名 String oldName = uploadFile.getOriginalFilename(); String newName = UUID.randomUUID().toString() + oldName.substring(oldName.lastIndexOf("."), oldName.length()); try { // 檔案儲存 uploadFile.transferTo(new File(folder, newName)); // 獲取上傳檔案的訪問路徑 String filePath = req.getScheme() + "://" + req.getServerName() + ":" + req.getServerPort() + "/uploadFile/" + format + newName; result += filePath + "<br>"; } catch (IOException e) { e.printStackTrace(); } } // 返回所有上傳檔案的訪問路徑 return result; } }

2,執行測試

(1)我們使用瀏覽器訪問uploads.html頁面並選擇檔案上傳,不過這次我們可以同時選擇多個檔案一起上傳:

(2)上傳成功後會返回所有上傳檔案的訪問路徑:

(3)通過這個訪問路徑我們就可以看到剛剛上傳的檔案:

附:常用引數配置

(1)如果我們需要對圖片上傳的細節進行配置,也是允許的。比如我們可以在application.properties檔案中新增如下配置:

spring.servlet.multipart.enabled=true
spring.servlet.multipart.file-size-threshold=0
spring.servlet.multipart.location=D:\\workspace
spring.servlet.multipart.max-file-size=1MB
spring.servlet.multipart.max-request-size=10MB
spring.servlet.multipart.resolve-lazily=false

yml版本

spring:
    servlet:
        multipart:
            enabled:true
            file-size-threshold:''
            location:D:\\workspace
            max-file-size:1MB
            max-request-size:10MB
            resolve-lazily:false
(2)上面幾個引數作用如下:
  • spring.servlet.multipart.enabled:表示是否開啟檔案上傳支援,預設為true
  • spring.servlet.multipart.file-size-threshold:表示檔案寫入磁碟的閥值,預設為0
  • spring.servlet.multipart.location:表示上傳檔案的臨時儲存位置
  • spring.servlet.multipart.max-file-size:表示上傳的單個檔案的最大大小,預設為1MB
  • spring.servlet.multipart.max-request-size:表示多檔案上傳時檔案的總大小,預設為10MB
  • spring.servlet.multipart.resolve-lazily:表示檔案是否延遲解析,預設為false
早年同窗始相知,三載瞬逝情卻萌。年少不知愁滋味,猶讀紅豆生南國。別離方知相思苦,心田紅豆根以生。