三種上傳方式
- 直接上傳到應用服務器
- 上傳到oss(內容存儲在服務器)如 阿里云 七牛云
- 前端將圖片轉成Base64編碼上傳(適于小容量的上傳)
SpringBoot文件上傳示例:——前后端不分離
創建upload模塊項目
新建步驟1.png
新建步驟2.png
-
添加web thymeleaf依賴
新建步驟3.png
新建步驟4.png
新建步驟5.png
最后新建成功
-
配置上傳屬性application.properties,指定上傳文件大小限制
image.png - 編寫Controller 通過Java.nio實現文件上傳
package com.springboot.upload.controller;
import org.springframework.stereotype.Controller;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;
//上傳文件控制器
//直接上傳到服務器
@Controller
public class UploadController {
//指定一個臨時路徑作為上傳目錄
//private static String UPLOAD_FOLDER = "C:\Users\Liuyu\Desktop\UPLOAD\";
//遇到[http://localhost:8080](https://links.jianshu.com/go?to=http%3A%2F%2Flocalhost%3A8080),則跳轉至upload.html頁面
@GetMapping("/")
public String index() {
return "upload";
}
@PostMapping("upload")
public String fileUpload(@RequestParam("file")MultipartFile srcFile, RedirectAttributes redirectAttributes) {
//前端沒有選擇文件,srcFile為空
if(srcFile.isEmpty()) {
redirectAttributes.addFlashAttribute("message", "請選擇一個文件");
return "redirect:upload_status";
}
//選擇了文件,開始上傳操作
try {
//構建上傳目標路徑,找到了項目的target的classes目錄
File destFile = new File(ResourceUtils.getURL("classpath:").getPath());
if(!destFile.exists()) {
destFile = new File("");
}
//輸出目標文件的絕對路徑
System.out.println("file path:"+destFile.getAbsolutePath());
//拼接子路徑
SimpleDateFormat sf_ = new SimpleDateFormat("yyyyMMddHHmmss");
String times = sf_.format(new Date());
File upload = new File(destFile.getAbsolutePath(), "picture/"+times);
//若目標文件夾不存在,則創建
if(!upload.exists()) {
upload.mkdirs();
}
System.out.println("完整的上傳路徑:"+upload.getAbsolutePath()+"/"+srcFile);
//根據srcFile大小,準備一個字節數組
byte[] bytes = srcFile.getBytes();
//拼接上傳路徑
//Path path = Paths.get(UPLOAD_FOLDER + srcFile.getOriginalFilename());
//通過項目路徑,拼接上傳路徑
Path path = Paths.get(upload.getAbsolutePath()+"/"+srcFile.getOriginalFilename());
//** 開始將源文件寫入目標地址
Files.write(path, bytes);
String uuid = UUID.randomUUID().toString().replaceAll("-", "");
// 獲得文件原始名稱
String fileName = srcFile.getOriginalFilename();
// 獲得文件后綴名稱
String suffixName = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
// 生成最新的uuid文件名稱
String newFileName = uuid + "."+ suffixName;
redirectAttributes.addFlashAttribute("message", "文件上傳成功"+newFileName);
} catch (IOException e) {
e.printStackTrace();
}
return "redirect:upload_status";
}
//匹配upload_status頁面
@GetMapping("upload_status")
public String uploadStatusPage() {
return "upload_status";
}
}
- 控制器配置好thymeleaf的頁面跳轉及信息顯示
upload.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Spring Boot文件上傳頁面</title>
</head>
<body>
<form method="post" action="/upload" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" value="上傳">
</form>
</body>
</html>
upload_status.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>文件上傳狀態顯示</title>
</head>
<body>
<h2>Spring Boot的文件上傳狀態</h2>
<div th:if="${message}">
<h2 th:text="${message}"/>
</div>
</body>
</html>
-
最后運行結果:
image.png
image.png