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

Spring Boot文件上传

配置文件上传属性:

在application.properties文件中配置文件上传的属性,包括上传目录的路径、文件大小限制等。

spring.servlet.multipart.max-file-size=10MB
spring.servlet.multipart.max-request-size=10MB

处理文件上传请求

上传的文件按照日期进行归类,使用UUID给文件重命名

    @PostMapping("/upload/")@ResponseBodypublic  Response upload(MultipartFile file) {// 验证是否有文件if(file == null || file.isEmpty()){return Response.newFail("Upload failed, please select file",400);}// 文件保存目录SimpleDateFormat sdf = new SimpleDateFormat("/yyyy/MM/dd/");String format = sdf.format(new Date());String filePath = "D:/flies/springboot/"+format;// 验证文件夹File folder = new File(filePath);if (!folder.exists()) {folder.mkdirs();}// 文件名String fileName = UUID.randomUUID() + file.getOriginalFilename();filePath = filePath  + fileName;File saveFile = new File(filePath);try {file.transferTo(saveFile);return  Response.newSuccess("Upload successful");} catch (IOException e) {e.printStackTrace();return  Response.newFail("Upload failed",50001);}}

文件过大

如果遇到文件过大出现413状态码无结果
需要统一返回json,可以参考
Springboot封装统一返回结果及全局异常处理

配置文件保存路径

可以在配置中保存文件的存放位置,方便更改

配置文件

application.properties文件添加需要的配置

file.path=D:\\flies\\springboot\\

@ConfigurationProperties 注解

使用注解@ConfigurationProperties将配置项和实体Bean关联起来,实现配置项和实体类字段的关联,读取配置文件数据。

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;@Data
@Component
@ConfigurationProperties(prefix = "file")
public class FileConfig {private String path;
}

使用

获取配置信息

FileConfig fileConfig = new FileConfig();
// 文件保存目录
String filePath = fileConfig.getPath();
    @PostMapping("/upload/")@ResponseBodypublic  Response upload(MultipartFile file) {// 验证是否有文件if(file == null || file.isEmpty()){return Response.newFail("Upload failed, please select file",400);}FileConfig fileConfig = new FileConfig();// 文件保存目录String filePath = fileConfig.getPath();// 验证文件夹File folder = new File(filePath);if (!folder.exists()) {folder.mkdirs();}// 文件名String fileName = UUID.randomUUID() + file.getOriginalFilename();filePath = filePath  + fileName;File saveFile = new File(filePath);try {file.transferTo(saveFile);return  Response.newSuccess("Upload successful");} catch (IOException e) {e.printStackTrace();return  Response.newFail("Upload failed",50001);}}
http://www.lryc.cn/news/449586.html

相关文章:

  • 基于Springboot+Vue的高校体育运动会比赛系统(含源码+数据库)
  • 【JavaEE】——内存可见性问题
  • YOLO训练参数设置解析
  • 基于OpenCV的实时年龄与性别识别(支持CPU和GPU)
  • 理解Js执行上下文
  • 微信小程序 蓝牙通讯
  • java后端项目技术记录
  • PostgreSQL数据库与PostGIS在Windows中的部署与运行
  • 高级算法设计与分析 学习笔记10 平摊分析
  • 从“纸面算力”到“好用算力”,超聚变打通AI+“最后一公里”
  • 【有啥问啥】具身智能(Embodied AI):人工智能的新前沿
  • 11-pg内核之锁管理器(六)死锁检测
  • Git 与标签管理
  • 【0334】Postgres内核之 auxiliary process(辅助进程)初始化 MyPgXact
  • 20.1 分析pull模型在k8s中的应用,对比push模型
  • Ubuntu 镜像替换为阿里云镜像:简化你的下载体验
  • The Sandbox 游戏制作教程第 6 章|如何使用装备制作出色的游戏 —— 避免环境危险
  • JavaScript中的输出方式
  • 力扣9.25
  • 从零开始之AI面试小程序
  • Html2OpenXml:HTML转化为OpenXml的.Net库,轻松实现Html转为Word。
  • HumanNeRF:Free-viewpoint Rendering of Moving People from Monocular Video 精读
  • Springboot中基于注解实现公共字段自动填充
  • Android 已经过时的方法用什么新方法替代?
  • 【RocketMQ】MQ与RocketMQ介绍
  • 【笔记】自动驾驶预测与决策规划_Part4_时空联合规划
  • Linux指令收集
  • 《C++并发编程实战》笔记(五)
  • 在Python中实现多目标优化问题(5)
  • 【Linux:共享内存】