一、單個檔案上傳
前端代碼可參考elementUI,后端代碼主要上傳一個檔案MultipartFile multipartFile
@PostMapping("/upload")
public ObjectRestResponse uploadKnowledge(@RequestParam(value = "https://www.cnblogs.com/jasmine-e/p/multipartFile") MultipartFile multipartFile) throws IOException {
Map<String, Object> map = new HashMap<String, Object>();
System.out.println("multipartFile.getOriginalFilename() = " + multipartFile.getOriginalFilename());
String originalFilename = multipartFile.getOriginalFilename();
String substring = originalFilename.substring(originalFilename.lastIndexOf('.'));
System.out.println(multipartFile.getName());
if (multipartFile != null) {
// 設定檔案名稱
map.put("nameParam", multipartFile.getName());
// 設定檔案名稱
map.put("fileName", multipartFile.getName());
// 設定檔案型別
map.put("contentType", multipartFile.getContentType());
// 設定檔案大小
map.put("fileSize", multipartFile.getSize());
// 創建檔案名稱
String fileName = UUID.randomUUID() + substring;
// 獲取到檔案的路徑資訊
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) requestAttributes;
//檔案存盤路徑
String filePath = "G:\\creo\\product\\" + fileName;
// 列印保存路徑
System.out.println(filePath);
// 保存檔案的路徑資訊,因為是本地存的,所以我回傳的是可以訪問的路徑,到時候查可以直接查出來,
// 將上傳檔案這個介面抽出來,可以復用,回傳的路徑可以存到相應的資料庫
map.put("filePath", "192.168.1.125:8765/product/" + fileName);
// 創建檔案
File saveFile = new File(filePath);
// 檔案保存
multipartFile.transferTo(saveFile);
}
return new ObjectRestResponse().data(map);
}
postman或者apiFox測驗如下:

二、批量上傳
批量上傳主要是傳一個路徑,然后一層一層的讀取檔案,將檔案上傳到服務器中,我的還是上傳到本地中,注意上傳檔案的大小是否有限制,如果報錯可以參考:DataBufferLimitException: Exceeded limit on max bytes to buffer : 262144
-
目標:上傳下面這個檔案夾

-
后端代碼測驗:
@Test
public void readFiles() throws Exception{
//上傳路徑
String path = "E:\\英語\\【初中英語教資面試】必看資料@Bilibili CocoPolaris";
readFilesDir(path);
}
//決議檔案夾
private void readFilesDir(String path) throws Exception {
LinkedList<File> dirList = new LinkedList<>();
LinkedList<String> fileList = new LinkedList<>();
File file = new File(path);
File[] files = file.listFiles();
for (File file1 : files) {
if (file1.isDirectory()){
dirList.add(file1);
}else {
FileInputStream fileInputStream = new FileInputStream(file1);
MockMultipartFile mockMultipartFile = new MockMultipartFile(file1.getName(), file1.getName(), "application/json", fileInputStream);
//System.out.println("mockMultipartFile = " + mockMultipartFile.getName());
fileList.add(file1.getAbsolutePath());
System.out.println("file1 = " + file1.getAbsolutePath());
// 創建檔案名稱
String fileName = UUID.randomUUID() + "."
+ file1.getName();
String filePath = "G:\\temp\\" + fileName;
// 列印保存路徑
System.out.println(filePath);
// 創建檔案
File saveFile = new File(filePath);
// 檔案保存
mockMultipartFile.transferTo(saveFile);
}
}
File temp;
while ((!dirList.isEmpty())){
temp=dirList.removeFirst();
if (temp.isDirectory()){
files = temp.listFiles();
if (files==null) continue;
for (File file1 : files) {
if (file1.isDirectory()){
dirList.add(file1);
}else {
FileInputStream fileInputStream = new FileInputStream(file1);
MockMultipartFile mockMultipartFile = new MockMultipartFile(file1.getName(), file1.getName(), "application/json", fileInputStream);
// System.out.println("mockMultipartFile = " + mockMultipartFile.getName());
fileList.add(file1.getAbsolutePath());
System.out.println("file1 = " + file1.getAbsolutePath());
// 創建檔案名稱
String fileName = UUID.randomUUID() + "."
+ file1.getName();
//存盤的路徑
String filePath = "G:\\temp\\" + fileName;
// 列印保存路徑
System.out.println(filePath);
// 創建檔案
File saveFile = new File(filePath);
// 檔案保存
mockMultipartFile.transferTo(saveFile);
}
}
}else {
System.out.println("======================");
}
}
}
- 結果:

轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/472229.html
標籤:Windows
