当前位置: 首页 > news >正文

SpringBoot 框架实现文件上传下载分享

SpringBoot 文件上传下载是我们常用的功能,比如图片、视频上传、下载和更新等功能的实现。下面我们详细分析一下:

1、pom.xml包引入

<!-- 基础 web 依赖(包含文件上传支持)  -->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId>
</dependency><!-- 文件操作工具类 -->
<dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>2.11.0</version>
</dependency>

2、存储目录设计

/data/project_files/
    ├── images/
    │   ├── 202405/
    │   └── 202406/
    └── videos/
        ├── 202405/
        └── 202406/

  • 使用 UUID 生成唯一文件名(保留原始扩展名)
  • 按年月生成子目录(防止单目录文件过多)
  • 推荐存储绝对路径到数据库(如:/images/202405/uuid123.jpg

3、配置文件信息

# win环境文件存储根目录 
file.upload-dir=D:/data/project_files/#linux环境
file.upload-dir=/data/project_files/# 上传大小限制(默认1MB,按需调整) 
spring.servlet.multipart.max-file-size=100MB
spring.servlet.multipart.max-request-size=100MB

4、上传接口

@PostMapping("/upload")
public ApiResult uploadFile(@RequestParam("file") MultipartFile file, @RequestParam String fileType) throws IOException {// 验证文件类型if (!Arrays.asList("image", "video").contains(fileType)) {return ApiResult.error("Invalid file type");}// 生成存储路径String datePath = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMM"));String fileExt = FilenameUtils.getExtension(file.getOriginalFilename());String uuid = UUID.randomUUID().toString();String relativePath = String.format("/%s/%s/%s.%s", fileType + "s", datePath, uuid, fileExt);// 保存文件File dest = new File(uploadDir + relativePath);FileUtils.forceMkdirParent(dest);file.transferTo(dest);return ApiResult.ok(relativePath);}

直接获取下载地址上传方式

 

    @PostMapping("/uploadFile")@ApiOperation(value="文件上传",notes = "文件上传-√")public String uploadFile(@RequestParam("file")MultipartFile file, @RequestParam String fileType) throws IOException {// 验证文件类型if (!Arrays.asList("image", "video").contains(fileType)) {return CommonResponse.buildErrorResponse("Error file type");}// 生成存储路径String datePath = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMM"));String fileExt = FilenameUtils.getExtension(file.getOriginalFilename());String uuid = UUID.randomUUID().toString();String relativePath = String.format("/%s/%s/%s.%s",fileType + "s", datePath, uuid, fileExt);// 保存文件File dest = new File(uploadDir + relativePath);FileUtils.forceMkdirParent(dest);file.transferTo(dest);log.info("filePath:{}",fileIpPortUrl+relativePath);return (fileIpPortUrl+relativePath);}

5、下载接口

1)接口下载

 @GetMapping("/download")public ResponseEntity<Resource> downloadFile(@RequestParam String filePath) throws IOException {Path path = Paths.get(uploadDir + filePath);Resource resource = new UrlResource(path.toUri());return ResponseEntity.ok().header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + resource.getFilename() + "\"").body(resource);}

2)原路径获取下载

    @GetMapping("/download/**")@ApiOperation(value="文件下载接口",notes = "文件下载接口-√")public ResponseEntity<Resource> downloadFile(HttpServletRequest request) throws IOException {// 获取完整文件路径(需截掉前缀路径)String filePath = request.getServletPath().replaceFirst("/file/******/", "");log.info("filePath:{}",filePath);// 路径安全校验if (filePath.contains("..")) {return ResponseEntity.badRequest().build();}Path path = Paths.get(uploadDir + File.separator + filePath);log.info("path:{}",path);Resource resource = new UrlResource(path.toUri());log.info("toUri:{}",path.toUri());if (!resource.exists()) {return ResponseEntity.notFound().build();}return ResponseEntity.ok().header(HttpHeaders.CONTENT_DISPOSITION,"attachment; filename=\"" + resource.getFilename() + "\"").body(resource);}

 6、修改文件

    @PostMapping("/update")public ApiResult updateFile(@RequestParam("file") MultipartFile file,@RequestParam String oldFilePath) throws IOException {// 删除旧文件File oldFile = new File(uploadDir + oldFilePath);if (oldFile.exists()) {FileUtils.forceDelete(oldFile);}// 上传新文件(复用上传逻辑)return uploadFile(file, parseFileType(oldFilePath));}

或者

    import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.UUID;@PostMapping("/update")public String updateFile(@RequestParam("file") MultipartFile file,@RequestParam String oldFilePath, @RequestParam String fileType) throws IOException {// 删除旧文件File oldFile = new File(uploadDir + oldFilePath);if (oldFile.exists()) {FileUtils.forceDelete(oldFile);}// 上传新文件(复用上传逻辑)return uploadFile(file, fileType);}

到此,文件上传下载分享完成,后期会分享站点上传数据,敬请期待。

http://www.xdnf.cn/news/926389.html

相关文章:

  • 泛型接口:允许在接口中使用类型参数
  • gis 高程影像切片地图发布geoserver
  • 深圳SMT贴片工艺优化关键步骤
  • 财务后台系统
  • Python Day44 学习(日志Day12复习)
  • 嵌入式部分BSP相关实现
  • LeetCode 每日一题 2025/6/2-2025/6/8
  • 从golang的sync.pool到linux的slab分配器
  • Android开发 系统签名jks制作和问题汇总
  • 实现简易动效
  • 杭州瑞盟 MS35774/MS35774A 低噪声256细分微步进电机驱动,用于空调风门电机驱动,香薰电机驱动
  • ViiTor实时翻译 2.4.2 | 完全免费的同声传译软件 实测识别率非常高 可以识别视频生成字幕
  • 看看不同主干的参数量是多少
  • 【Linux】SSH:免密登录
  • Egg.js框架的基本介绍与用法,以及如何连接数据库并对数据库进行增删改查
  • Go 语言中的 make 函数详解
  • AI推理服务的高可用架构设计
  • 第9篇:数据库中间件的容错机制与高可用架构设计
  • 负载均衡--堆/优先队列模拟
  • 抗辐照MCU在卫星载荷电机控制器中的实践探索
  • SDC命令详解:使用set_propagated_clock命令进行约束
  • JDK21深度解密 Day 14:生产环境监控与排错
  • 什么是hint热点行更新呢?
  • matlab 2024a ​工具箱Aerospsce Toolbox报错​
  • 【Linux】Linux进程间通讯-共享内存
  • curl 如何发送一个邮件 ?
  • selenium自动化测试学习心得1
  • 阿里巴巴ROLL:大规模强化学习优化的高效易用解决方案
  • CDTJDT是开发SAST工具的有力引擎
  • Java 并发编程系列(上篇):多线程深入解析