springboot实现多文件上传

devtools/2025/2/26 20:25:20/

springboot实现多文件上传

代码

java">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.ppmy.cn/devtools/162879.html

相关文章

计算机毕业设计SpringBoot+Vue.js购物推荐系统网站(源码+文档+PPT+讲解)

温馨提示&#xff1a;文末有 CSDN 平台官方提供的学长联系方式的名片&#xff01; 温馨提示&#xff1a;文末有 CSDN 平台官方提供的学长联系方式的名片&#xff01; 温馨提示&#xff1a;文末有 CSDN 平台官方提供的学长联系方式的名片&#xff01; 作者简介&#xff1a;Java领…

硬件工程师入门教程

1.欧姆定律 测电压并联使用万用表测电流串联使用万用表&#xff0c;红入黑出 2.电阻的阻值识别 直插电阻 贴片电阻 3.电阻的功率 4.电阻的限流作用 限流电阻阻值的计算 单位换算关系 5.电阻的分流功能 6.电阻的分压功能 7.电容 电容简单来说是两块不连通的导体加上中间的绝…

DeepSeek 提示词:基础结构

&#x1f9d1; 博主简介&#xff1a;CSDN博客专家&#xff0c;历代文学网&#xff08;PC端可以访问&#xff1a;https://literature.sinhy.com/#/?__c1000&#xff0c;移动端可微信小程序搜索“历代文学”&#xff09;总架构师&#xff0c;15年工作经验&#xff0c;精通Java编…

Spring Boot中如何使用Thymeleaf模板引擎

Thymeleaf 是一个现代化的服务器端 Java 模板引擎,在 Spring Boot 项目中使用它可以方便地将 Java 代码和 HTML 页面进行整合,生成动态的 Web 页面。以下将详细介绍在 Spring Boot 中如何使用 Thymeleaf 模板引擎。 1. 添加依赖 如果你使用的是 Maven 项目,在 pom.xml 中添…

C# 封装

C# 封装 引言 封装(Encapsulation)是面向对象编程(OOP)中的一个核心概念,它指的是将对象的属性(数据)和操作(函数)捆绑在一起,形成独立的单元,从而隐藏对象的内部细节,只提供公共接口供外部访问。在C#中,封装是实现信息隐藏和数据保护的一种有效手段,可以提高代…

C++day6

编写一个如下场景&#xff1a; 有一个英雄Hero类&#xff0c;私有成员&#xff0c;攻击&#xff0c;防御&#xff0c;速度&#xff0c;生命值&#xff0c;以及所有的set get 方法 编写一个 武器 Weapon 类&#xff0c;拥有私有成员攻击力&#xff0c;以及set get 方法 编写一个…

DeepSeek开源周第二日-DeepEP

&#x1f680;deepseek开源周第二天&#xff0c;DeepEP&#xff1a;专为MoE和专家并行打造的高性能通信库 &#x1f525;DeepEP 主要特点 &#x1f4a1; 高效 GPU 通信内核&#xff1a;提供高吞吐、低延迟的 all-to-all GPU 内核&#xff08;MoE dispatch & combine&…

Ansible-03 docker安装-基于centos

1.创建invertory.ini并验证连通性 创建invertory.ini [docker_servers] 192.168.0.2执行命令 ansible -i invertory.ini docker_servers -m ping结果 192.168.0.2 | SUCCESS > {"ansible_facts": {"discovered_interpreter_python": "/usr/lib…