springboot+thymeleaf 文件上传功能的实现代码
作者:weize_hh 时间:2023-11-25 05:08:59
pom.xml
<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>
application.yml
spring:
servlet:
multipart:
#上传文件总的最大值
max-request-size: 10MB
#上传文件的最大值
max-file-size: 10MB
index.html 文件上传页面
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>文件上传</title>
</head>
<body>
<p>单文件上传</p>
<form method="post" action="/upload" enctype="multipart/form-data">
<p><input type="file" name="file00"></p>
<p><span th:text="${msg}"></span></p>
<input type="submit" value="提交">
</form>
<hr/>
<p>多文件上传</p>
<form method="post" enctype="multipart/form-data" action="/batch">
<p>文件1:<input type="file" name="file"/></p>
<p>文件2:<input type="file" name="file"/></p>
<p><input type="submit" value="上传"/></p>
</form>
</body>
</html>
hello.html 上传成功的页面
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<p>单文件上传</p>
<p th:text="${msg}"></p>
<hr>
<p>多文件上传</p>
<ul>
<li th:each="msg1:${msgList}" th:text="${msg1}"></li>
</ul>
</body>
</html>
controller: 文件上传
import org.springframework.core.io.ResourceLoader;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartRequest;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
@Controller
public class FileUploadController {
//单一文件上传
@RequestMapping("/upload")
public String uploadFile(@RequestParam("file00") MultipartFile file, Model model){
String msg="";
try {
if(file.isEmpty()){
model.addAttribute("msg","上传失败,请选择文件!");
return "index";
}
String filename = file.getOriginalFilename();
//String filePath = request.getServletContext().getRealPath("/upload");
String filePath = ResourceUtils.getURL("classpath:").getPath()+"static/";
//避免文件重复覆盖
String uuid= UUID.randomUUID().toString().replaceAll("-", "");
//时间戳分类文件
String time = new SimpleDateFormat("YYYY-MM").format(new Date());
String realPath = filePath+time+"/"+uuid+filename;
File dest = new File(realPath);
//检测是否存在目录,无,则创建
if(!dest.getParentFile().exists()){
dest.getParentFile().mkdirs();//新建文件夹 多级目录
}
file.transferTo(dest);//文件写入
} catch (IOException e) {
e.printStackTrace();
}
model.addAttribute("msg","文件上传成功!");
return "hello";
}
//多文件上传
@RequestMapping("/batch")
public String uploadMoreFiles(HttpServletRequest request, Model model){
MultipartRequest request1 = (MultipartRequest)request;
//猜测 file为 input 类型为 file
List<MultipartFile> fileList = request1.getFiles("file");
List<String> msgList = new ArrayList<>();
System.out.println(fileList.size());
try {
String filepath = ResourceUtils.getURL("classpath:").getPath()+"static/";
for (int i=1;i<=fileList.size();i++){
MultipartFile file = fileList.get(i-1);
if (file.isEmpty()){
msgList.add("上传第"+i+"个文件失败");
model.addAttribute("msgList",msgList);
continue;
}
String filename = file.getOriginalFilename();
//避免文件重复覆盖
String uuid= UUID.randomUUID().toString().replaceAll("-", "");
//时间戳分类文件
String time = new SimpleDateFormat("YYYY-MM").format(new Date());
String realPath = filepath+time+"s/"+uuid+filename;
File dest = new File(realPath);
//System.out.println("realPath"+realPath);
//检测是否存在目录,无,则创建
if(!dest.getParentFile().exists()){
dest.getParentFile().mkdirs();//新建文件夹 多级目录
}
msgList.add("第"+i+"个文件,上传成功!");
file.transferTo(dest);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
model.addAttribute("msgList",msgList);
return "hello";
}
}
测试:
注:目前仅实现了文件的上传
计划补充:文件下载+上传的图片展示;
上传的图片展示:
遇到的问题: 直接使用 realPath 作为图片拼接地址 浏览器报 安全错误
使用字符串拼接,也会报错404
index = realPath.lastIndexOf("static");
upFilePaths.add("../"+realPath.substring(index));
来源:https://blog.csdn.net/weize_hh/article/details/110118814
标签:springboot,thymeleaf,文件上传
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
MyBatis多表关联查询的实现示例
2023-07-06 06:26:49
![](https://img.aspxhome.com/file/2023/8/88748_0s.jpg)
Java通过jersey实现客户端图片上传示例
2022-09-17 15:44:24
![](https://img.aspxhome.com/file/2023/4/86804_0s.jpg)
JAVA Map架构和API介绍
2023-01-25 14:01:38
![](https://img.aspxhome.com/file/2023/7/129167_0s.jpg)
C#反射应用实例
2023-11-03 14:47:46
![](https://img.aspxhome.com/file/2023/9/98209_0s.png)
Seata 环境搭建部署过程
2021-09-06 08:45:36
![](https://img.aspxhome.com/file/2023/8/78278_0s.png)
深入分析Java内存区域的使用详解
2023-09-25 23:21:29
关于java的九个预定义Class对象
2023-08-16 01:56:16
Android 双击Back键退出应用的实现方法
2023-07-06 05:41:41
Spring Boot与Spring MVC Spring对比及核心概念
2023-09-27 05:14:36
![](https://img.aspxhome.com/file/2023/0/70960_0s.png)
Java实现简单猜拳游戏
2023-05-27 18:28:16
Android自定义输入法软键盘
2023-04-20 05:51:54
![](https://img.aspxhome.com/file/2023/1/99791_0s.jpg)
2022 最新 IntelliJ IDEA 详细配置步骤演示(推荐)
2021-11-20 21:05:48
![](https://img.aspxhome.com/file/2023/3/77483_0s.jpg)
Android Fragment的用法实例详解
2022-01-21 03:39:40
![](https://img.aspxhome.com/file/2023/3/126743_0s.png)
Android使用SQLite数据库的简单实例
2022-03-04 13:52:50
![](https://img.aspxhome.com/file/2023/7/106047_0s.bmp)
SpringBoot项目中分页插件PageHelper无效的问题及解决方法
2021-07-08 23:25:55
JAVA Spring中让人头痛的JAVA大事务问题要如何解决你知道吗
2023-01-14 04:58:04
![](https://img.aspxhome.com/file/2023/8/67948_0s.jpg)
C#多线程中的互斥锁Mutex
2022-04-26 06:11:55
![](https://img.aspxhome.com/file/2023/5/69225_0s.png)
java实现Base64加密解密算法
2023-11-25 08:07:27
通过FeignClient调用微服务提供的分页对象IPage报错的解决
2022-01-27 20:19:23
![](https://img.aspxhome.com/file/2023/7/63057_0s.png)
C#操作ftp类完整实例
2021-09-02 22:45:30