# SpringBoot+thymeleaf 实现文件上传
**Repository Path**: cheng-nian/springBoot_thymeleaf_upload_file
## Basic Information
- **Project Name**: SpringBoot+thymeleaf 实现文件上传
- **Description**: SpringBoot+thymeleaf 实现文件上传
- **Primary Language**: Java
- **License**: MIT
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 1
- **Created**: 2023-12-04
- **Last Updated**: 2023-12-04
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
# SpringBoot + thymeleaf 实现文件上传
## 1.目录结构

## 2.upload.html
```html
SpringBoot上传文件
测试SpringBoot文件上传
```
## 3.FileController.java
```java
package xin.baizhiedu.controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
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;
@Controller
@RequestMapping("/file")
public class FileController {
private static final Logger log = LoggerFactory.getLogger(FileController.class);
@Value("${file.upload.dir}")
private String realPath;
/**
* 第二种文件上传
* 注意:这种方式适用于任何一种部署方式 推荐使用这种方式
*
* @param file
* @return
* @throws IOException
*/
@RequestMapping("/uploadByJarDeploy")
// 定义:接收文件对象 MultipartFile file变量名要与form表单中input type="file" 标签name属性名一致
public String uploadByJarDeploy(MultipartFile file) throws IOException {
// 文件名
String originalFilename = file.getOriginalFilename();
log.debug("文件名: {}", file.getOriginalFilename());
log.debug("文件大小: {}", file.getSize());
log.debug("文件类型: {}", file.getContentType());
// 改文件名
String ext = originalFilename.substring(originalFilename.lastIndexOf("."));
String newFileName = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss-SSS-").format(new Date()) + UUID.randomUUID() + ext;
// 上传文件到哪
file.transferTo(new File(realPath, newFileName));
return "redirect:/";
}
/**
* 用来测试文件上传 - 这种方式:不能用于jar包部署
* 注意:这种方式存在局限性, 不推荐再使用这种方式进行文件上传了
*
* @return
*/
/*
@RequestMapping("/uploadFile")
// 定义:接收文件对象 MultipartFile file变量名要与form表单中input type="file" 标签name属性名一致
public String uploadFileTest(MultipartFile file, HttpServletRequest request) throws IOException {
// 文件名
String originalFilename = file.getOriginalFilename();
log.debug("文件名: {}", file.getOriginalFilename());
log.debug("文件大小: {}", file.getSize());
log.debug("文件类型: {}", file.getContentType());
// 处理文件上传
// 1.根据相对路径 "uploads" 获取绝对路径
String realPath = request.getSession().getServletContext().getRealPath("/uploads");
log.debug("获取的绝对路径: {}", realPath);
// 2.上传文件 参数1:将文件写入到那个目录
// 修改文件名
String ext = originalFilename.substring(originalFilename.lastIndexOf("."));
String newFileName = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss-SSS-").format(new Date()) + UUID.randomUUID() + ext;
file.transferTo(new File(realPath, newFileName));
return "redirect:/";
}
*/
}
```
## 4.application.yml
```yml
server:
port: 8080
# servlet:
# context-path: /springboot-file
spring:
servlet:
multipart: # # 修改文件上传的大小限制
max-request-size: 1024MB # 运行请求传递文件大小最大为1024MB
max-file-size: 1024MB # 运行服务器可以处理的最大文件大小1024MB
profiles:
active: local # 激活本地配置生效
logging:
level:
root: info
xin.baizhiedu.controller: debug
# 指定文件上传位置
#file:
# upload:
# dir: D:\temp\1 # 指定本地测试上传目录
```
## 5.application-local.yml
```yaml
# 指定文件上传位置
file:
upload:
dir: D:\temp\1 # 指定本地测试上传目录 (Windows系统)
```
## 6.application-prod.yml
```yaml
# 指定文件上传位置
file:
upload:
dir: /home/user/uploads # 指定服务器测试上传目录(Linux系统)
```
## 7.pom.xml
```xml
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.5.0
xin.baizhiedu
springboot_day05-uploadFiles
0.0.1-SNAPSHOT
springboot_day05-uploadFiles
Demo project for Spring Boot
1.8
org.springframework.boot
spring-boot-starter-thymeleaf
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-maven-plugin
```
## 8.测试




