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

springboot实现多文件上传

springboot实现多文件上传

代码

package com.sh.system.controller;import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.List;
import java.util.UUID;@RestController
@RequestMapping("/filesUpload")
public class FileUploadController {// 文件存储路径(可以根据需要修改)private static String UPLOADED_FOLDER = "uploads/";/*** 单文件上传*/@PostMapping("/file")public ResponseEntity<String> uploadFile(@RequestParam("file") MultipartFile file) {if (file.isEmpty()) {return new ResponseEntity<>("Please select a file to upload.", HttpStatus.BAD_REQUEST);}try {// 获取文件名并添加唯一后缀防止重名String fileName = StringUtils.cleanPath(file.getOriginalFilename());String uniqueFileName = UUID.randomUUID().toString() + "_" + fileName;// 创建文件存储路径Path filePath = Paths.get(UPLOADED_FOLDER).toAbsolutePath().normalize();Files.createDirectories(filePath);// 保存文件到指定路径Path targetLocation = filePath.resolve(uniqueFileName);Files.copy(file.getInputStream(), targetLocation, StandardCopyOption.REPLACE_EXISTING);return ResponseEntity.ok("File uploaded successfully: " + uniqueFileName);} catch (IOException ex) {return new ResponseEntity<>("Could not upload the file!", HttpStatus.INTERNAL_SERVER_ERROR);}}/*** 多文件上传** */@PostMapping("/files")public ResponseEntity<String> uploadFiles(@RequestParam("files") List<MultipartFile> files) {if (files.isEmpty()) {return new ResponseEntity<>("Please select files to upload.", HttpStatus.BAD_REQUEST);}try {for (MultipartFile file : files) {// 获取文件名String fileName = StringUtils.cleanPath(file.getOriginalFilename());// 如果文件名为空或只包含空白字符,则生成一个唯一的文件名if (fileName.contains("..") || fileName.isEmpty()) {fileName = UUID.randomUUID().toString() + "_" + file.getOriginalFilename();}// 构建文件路径Path filePath = Paths.get(UPLOADED_FOLDER + fileName);// 确保上传目录存在Files.createDirectories(filePath.getParent());// 保存文件到指定路径Files.copy(file.getInputStream(), filePath, java.nio.file.StandardCopyOption.REPLACE_EXISTING);}return new ResponseEntity<>("Files uploaded successfully!", HttpStatus.OK);} catch (IOException e) {e.printStackTrace();return new ResponseEntity<>("Failed to upload files.", HttpStatus.INTERNAL_SERVER_ERROR);}}
}

postman操作截图
1、设置header
在这里插入图片描述
2、设置Body(说明:图示为多文件测试,单个文件,只需要设置一个key,value即可)
在这里插入图片描述

http://www.lryc.cn/news/542991.html

相关文章:

  • Webpack打包优化
  • 浅谈HTTP及HTTPS协议
  • GTID的基本概念
  • .NET Core MVC IHttpActionResult 设置Headers
  • 数据结构与算法面试专题——桶排序
  • 深度学习奠基作 AlexNet 论文阅读笔记(2025.2.25)
  • MongoDB 数据库简介
  • Transformer LLaMA
  • 【DeepSeek开源:会带来多大的影响】
  • Redis7——基础篇(七)
  • 边缘计算:通俗易懂的全方位解析
  • Flink 中的滚动策略(Rolling Policy)
  • GPU和FPGA的区别
  • 网易云音乐分布式KV存储实践与演进
  • WordPress平台如何接入Deepseek,有效提升网站流量
  • 【嵌入式】STM32内部NOR Flash磨损平衡与掉电保护总结
  • 什么是磁盘阵列(RAID)?如何提高磁盘阵列的性能
  • 轻量级日志管理平台Grafana Loki
  • k8s集群部署
  • STM32MP157A-FSMP1A单片机移植Linux系统SPI总线驱动
  • 系统基础与管理(2025更新中)
  • Python--内置函数与推导式(下)
  • 可狱可囚的爬虫系列课程 14:10 秒钟编写一个 requests 爬虫
  • Windows golang安装和环境配置
  • IP-------GRE和MGRE
  • LabVIEW形状误差测量系统
  • django校园互助平台~源码
  • Vue进阶之AI智能助手项目(五)——ChatGPT的调用和开发
  • Jenkins重启后Maven的Project加载失败
  • 【docker】docker pull拉取中不断重复下载问题,解决方案之一,磁盘空间扩容