当前位置: 代码网 > it编程>编程语言>Java > SpringBoot自定义配置项过程

SpringBoot自定义配置项过程

2024年11月08日 Java 我要评论
spring boot自定义配置项配置文件在application.properties文件添加需要的配置比如:file.path=d:\\flies\\springboot\\@configurat

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);
        }
    }

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com