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

Spring Boot自定义配置项

Spring Boot自定义配置项

配置文件

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/")
    @ResponseBody
    public  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.kler.cn/news/317068.html

相关文章:

  • [leetcode刷题]面试经典150题之6轮转数字(简单)
  • k8s上安装prometheus
  • 字母与符号检测系统源码分享
  • ubuntu、linux安装redis(使用tar包的方式)
  • 前端——实现时钟 附带小例子
  • 数据结构:线性表
  • 2024从传统到智能,AI做PPT软件的崛起之路
  • 【文心智能体】 旅游手绘手帐 开发分享 零代码 手绘风景 记录行程和心情 旅游攻略
  • 鹏哥C语言49---第5次作业:选择语句 if 和 switch
  • 脚本注入网页:XSS
  • springboot中的异步任务
  • Matplotlib-数据可视化详解
  • 瑞芯微RK3588开发板Linux系统添加自启动命令的方法,深圳触觉智能Arm嵌入式鸿蒙硬件方案商
  • git show 命令
  • Unity中Rigidbody 刚体组件和Rigidbody类是什么?
  • 【flex-shrink】计算 flex弹性盒子的子元素的宽度大小
  • 【27】C++项目练习
  • 循环中用sleep
  • linux atomic 原子变量操作
  • 【Python报错已解决】AttributeError: ‘WindowsPath‘ object has no attribute ‘rstrip‘
  • 生成式AI:ChatGPT及其在各行业的应用前景
  • git学习报告
  • 深入探索迭代器模式的原理与应用
  • 从零开始写一个建立FAT32文件系统程序
  • MFC - 复杂控件_2
  • 【安装教程】Windows环境下Apache Jena Fuseki的安装与配置
  • qt-C++笔记之作用等同的宏和关键字
  • 模拟电路工程师面试题
  • 如何解决npm下载Puppeteer卡死的问题
  • YOLOv9改进策略【注意力机制篇】| 2024 SCI TOP FCAttention 即插即用注意力模块,增强局部和全局特征信息交互