基于SpringBoot的文件上傳
上傳方式:
- 1.直接上傳到應(yīng)用服務(wù)器
- 2.上傳到OSS(阿里云、七牛云)
- 3.前端將圖片轉(zhuǎn)成Base64編譯上傳(僅用于小容量圖片)
使用第一種方式的步驟
-
1、創(chuàng)建upload模塊
image.png
填寫模塊名和包名
image.png
注意勾選web和thymeleaf選項(xiàng)
image.png
image.png - 2、添加web、thymeleaf依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
- 3、限制文件大小(在resources目錄下的application.properties下寫限制條件)
spring.servlet.multipart.max-file-size=100MB
- 4、使用java.nio.files實(shí)現(xiàn)文件的上傳(在com.spring.upload包下建一個(gè)controller包,controller包下建一個(gè)UploadController類,在UploadController下編寫代碼)
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;
/**
* 上傳文件控制器
* 直接上傳到服務(wù)器
*/
@Controller
public class UploadController {
//制定一個(gè)臨時(shí)路徑
//private static String UPLOAD_FOLDER="E:/temp/";
//遇到http://localhost:8080則跳轉(zhuǎn)到upload.html
@GetMapping("/")
public String index(){
return "upload";
}
@PostMapping("/upload")
public String fileUpload(@RequestParam("file") MultipartFile srcFile, RedirectAttributes redirectAttributes){
if(srcFile.isEmpty()){
redirectAttributes.addFlashAttribute("message","選擇一個(gè)文件");
return "redirect:upload_status";
}
try {
File destFile=new File(ResourceUtils.getURL("classpath").getPath());
if(destFile.exists()){
destFile=new File("");
}
//輸出目標(biāo)文件的絕對(duì)路徑
System.out.println("file path:"+destFile.getAbsolutePath());
//拼接子路經(jīng)
File upload=new File(destFile.getAbsolutePath(),"static/");
//目標(biāo)文件不存在,創(chuàng)建一個(gè)
if(!upload.exists()){
upload.mkdirs();
}
System.out.println("完整的上傳路徑:"+upload.getAbsolutePath()+"/"+srcFile.getOriginalFilename());
byte[] bytes=srcFile.getBytes();
//拼接上傳路徑
//Path path= Paths.get(UPLOAD_FOLDER+srcFile.getOriginalFilename());
//通過項(xiàng)目路徑,拼接上傳路徑
Path path=Paths.get(upload.getAbsolutePath()+"/"+srcFile.getOriginalFilename());
Files.write(path,bytes);
redirectAttributes.addFlashAttribute("message","文件上傳成功");
}catch (IOException e){
e.printStackTrace();
}
return "redirect:upload_status";
}
//匹配upload_status
@GetMapping("/upload_status")
public String uploadStatusPage(){
return "upload_status";
}
}
- 5、resources->templates下建html文件
- 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>文件上傳狀態(tài)顯示</title>
</head>
<body>
<h2>spring boot文件上傳狀態(tài)</h2>
<div th:if="${message}">
<h2 th:text="${message}"></h2>
</div>
</body>
</html>
運(yùn)行結(jié)果

image.png
選擇文件

image.png
上傳

image.png
查看文件夾(上傳成功)

image.png



