三種上傳方式
- 直接上傳到應(yīng)用服務(wù)器
- 上傳到oss(內(nèi)容存儲(chǔ)在服務(wù)器)如 阿里云 七牛云
- 前端將圖片轉(zhuǎn)成Base64編碼上傳(適于小容量的上傳)
SpringBoot文件上傳示例:——前后端不分離
創(chuàng)建upload模塊項(xiàng)目

新建步驟1.png

新建步驟2.png
-
添加web thymeleaf依賴(lài)
新建步驟3.png
新建步驟4.png
新建步驟5.png
最后新建成功
-
配置上傳屬性application.properties,指定上傳文件大小限制
image.png - 編寫(xiě)Controller 通過(guò)Java.nio實(shí)現(xiàn)文件上傳
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;
//上傳文件控制器
//直接上傳到服務(wù)器
@Controller
public class UploadController {
//指定一個(gè)臨時(shí)路徑作為上傳目錄
//private static String UPLOAD_FOLDER = "C:\Users\Liuyu\Desktop\UPLOAD\";
//遇到[http://localhost:8080](https://links.jianshu.com/go?to=http%3A%2F%2Flocalhost%3A8080),則跳轉(zhuǎn)至upload.html頁(yè)面
@GetMapping("/")
public String index() {
return "upload";
}
@PostMapping("upload")
public String fileUpload(@RequestParam("file")MultipartFile srcFile, RedirectAttributes redirectAttributes) {
//前端沒(méi)有選擇文件,srcFile為空
if(srcFile.isEmpty()) {
redirectAttributes.addFlashAttribute("message", "請(qǐng)選擇一個(gè)文件");
return "redirect:upload_status";
}
//選擇了文件,開(kāi)始上傳操作
try {
//構(gòu)建上傳目標(biāo)路徑,找到了項(xiàng)目的target的classes目錄
File destFile = new File(ResourceUtils.getURL("classpath:").getPath());
if(!destFile.exists()) {
destFile = new File("");
}
//輸出目標(biāo)文件的絕對(duì)路徑
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);
//若目標(biāo)文件夾不存在,則創(chuàng)建
if(!upload.exists()) {
upload.mkdirs();
}
System.out.println("完整的上傳路徑:"+upload.getAbsolutePath()+"/"+srcFile);
//根據(jù)srcFile大小,準(zhǔn)備一個(gè)字節(jié)數(shù)組
byte[] bytes = srcFile.getBytes();
//拼接上傳路徑
//Path path = Paths.get(UPLOAD_FOLDER + srcFile.getOriginalFilename());
//通過(guò)項(xiàng)目路徑,拼接上傳路徑
Path path = Paths.get(upload.getAbsolutePath()+"/"+srcFile.getOriginalFilename());
//** 開(kāi)始將源文件寫(xiě)入目標(biāo)地址
Files.write(path, bytes);
String uuid = UUID.randomUUID().toString().replaceAll("-", "");
// 獲得文件原始名稱(chēng)
String fileName = srcFile.getOriginalFilename();
// 獲得文件后綴名稱(chēng)
String suffixName = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
// 生成最新的uuid文件名稱(chēng)
String newFileName = uuid + "."+ suffixName;
redirectAttributes.addFlashAttribute("message", "文件上傳成功"+newFileName);
} catch (IOException e) {
e.printStackTrace();
}
return "redirect:upload_status";
}
//匹配upload_status頁(yè)面
@GetMapping("upload_status")
public String uploadStatusPage() {
return "upload_status";
}
}
- 控制器配置好thymeleaf的頁(yè)面跳轉(zhuǎn)及信息顯示
upload.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Spring Boot文件上傳頁(yè)面</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>文件上傳狀態(tài)顯示</title>
</head>
<body>
<h2>Spring Boot的文件上傳狀態(tài)</h2>
<div th:if="${message}">
<h2 th:text="${message}"/>
</div>
</body>
</html>
-
最后運(yùn)行結(jié)果:
image.png
image.png





