SpringBoot上传文件并配置本地资源映射来访问文件的实例代码
作者:weixin_44953227 时间:2023-07-24 02:41:08
1.准备工作
1、新建一个SpringBoot项目加上web依赖, 所有依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
2、application.properties
# 上传文件总的最大值
spring.servlet.multipart.max-request-size=10MB
#spring.servlet.multipart.max-request-size=10KB
# 单个文件的最大值
spring.servlet.multipart.max-file-size=10MB
#spring.servlet.multipart.max-file-size=10KB
# 文件上传路径, 当前项目根目录下的 uploadFile 目录
pro.uploadPath=uploadFile
3、新建文件上传的静态文件 index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>文件上传</title>
</head>
<body>
<h1>文件上传</h1>
<form method="post" action="/file/upload" enctype="multipart/form-data">
<input type="file" name="file"><br>
<input type="submit" value="提交">
</form>
</body>
</html>
2. 配置本地资源映射路径 addResourceHandlers
package com.pro.conf;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebMvcConf implements WebMvcConfigurer {
@Value("${pro.uploadPath}")
private String uploadPath;
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler(uploadPath + "/**") // 添加访问路径
// file: 当前项目根目录, 映射到真实的路径下的 uploadPath(变量)目录下
.addResourceLocations("file:" + uploadPath + "/");
}
}
3.上传代码
package com.pro.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;
@RestController
public class UploadController {
@Value("${pro.uploadPath}")
private String uploadPath;
/**
* 获取文件路径
* @param request 请求体
* @param fileName 文件名称
* @return 文件路径
*/
public String getUploadPath(HttpServletRequest request, String fileName) {
return request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + "/" + uploadPath + "/" + fileName;
}
// 文件上传, 上传到当前项目根目录下的 uploadFile 目录
@RequestMapping("/file/upload")
public Object upload(@RequestParam("file") MultipartFile multipartFile, HttpServletRequest request) {
Map<String, Object> map = new HashMap();
map.put("msg", "上传成功");
if (multipartFile.isEmpty()) {
map.put("msg", "空文件");
return map;
}
// 获取文件名
String fileName = multipartFile.getOriginalFilename();
if ("".equals(fileName)) {
map.put("msg", "文件名不能为空");
return map;
}
System.out.println("上传文件原始的名字: " + fileName);
// 使用uuid生成新文件名
String newFileName = UUID.randomUUID().toString().replaceAll("-", "") + fileName.substring(fileName.lastIndexOf("."), fileName.length());
System.out.println("保存的文件的新名字: " + newFileName);
// 获取年月日的日期格式
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
String format = simpleDateFormat.format(new Date());
// 生成以日期分割的文件路径
File readPath = new File(uploadPath + File.separator + format);
System.out.println("存放的文件夹: " + readPath);
System.out.println("存放文件的绝对路径: " + readPath.getAbsolutePath());
// 判断文件夹是否存在
if (!readPath.isDirectory()) {
// 创建文件夹
readPath.mkdirs();
}
// 文件真实的保存路径
File file = new File(readPath.getAbsolutePath() + File.separator + newFileName);
try {
multipartFile.transferTo(file);
// 获取存储路径
String filePath = getUploadPath(request, format + "/" + newFileName);
map.put("path", filePath);
} catch (IOException e) {
e.printStackTrace();
map.put("msg", "上传失败");
}
return map;
}
}
来源:https://blog.csdn.net/weixin_44953227/article/details/116013637
标签:SpringBoot,上传文件,资源映射
0
投稿
猜你喜欢
Android实现文件解压带进度条功能
2023-02-26 19:01:05
Java Spring事务的隔离级别详解
2022-04-25 23:07:52
java文件读写工具类分享
2023-07-30 21:04:19
C#客户端程序Visual Studio远程调试的方法详解
2022-10-20 19:13:54
javafx tableview鼠标触发更新属性详解
2022-01-24 23:47:32
Java Runnable线程传参,实现让run访问参数
2023-09-04 21:36:42
MyBatis学习教程(五)-实现关联表查询方法详解
2021-06-23 21:04:04
SpringBoot文件上传大小设置方式(yml中配置)
2021-09-21 09:26:47
Android多个TAB选项卡切换效果
2022-04-10 03:03:15
opencv3/C++ FLANN特征匹配方式
2021-08-19 10:17:47
Android自定义ViewGroup实现弹性滑动效果
2022-07-25 19:44:08
Room Kotlin API的使用入门教程
2023-11-07 07:06:35
springmvc实现简单的拦截器
2022-02-27 05:13:39
Hystrix Dashboard断路监控仪表盘的实现详细介绍
2022-07-29 05:27:26
Android 自定义组件成JAR包的实现方法
2023-04-08 17:56:39
C#设置页面单位和缩放的方法
2023-06-09 12:59:27
Android TextView中文字通过SpannableString设置属性用法示例
2023-07-26 07:11:51
C# Socket编程实现简单的局域网聊天器的示例代码
2022-10-01 19:45:45
使用Jenkins来构建SVN+Maven项目的实现
2023-07-30 12:45:22
android layout 按比例布局的代码
2022-10-11 13:02:40