zoukankan      html  css  js  c++  java
  • springboot获取项目的绝对路径和根目录

    原文:https://blog.csdn.net/f45056231p/article/details/88692444

    System.getProperty("user.dir")
    
    输出目录:  G:outshinewangsoso
    
    //获取classes目录绝对路径
    
    String path = ClassUtils.getDefaultClassLoader().getResource("").getPath();
    
    String path = ResourceUtils.getURL("classpath:").getPath();
    
    输出目录:  /G:/outshine/wangsoso/target/classes/
    
    //如果上传目录为/static/images/upload/,则可以如下获取:
    File upload = new File(path.getAbsolutePath(),"static/images/upload/");
    if(!upload.exists()) upload.mkdirs();
    System.out.println("upload url:"+upload.getAbsolutePath());
    //在开发测试模式时,得到的地址为:{项目跟目录}/target/static/images/upload/
    //在打包成jar正式发布时,得到的地址为:{发布jar包目录}/static/images/upload/
    
    注意:以jar包发布项目时,我们存储的路径是与jar包同级的static目录,因此我们需要在jar包目录的application.properties配置文件中设置静态资源路径,如下所示:
    
    #设置静态资源路径,多个以逗号分隔
    
    spring.resources.static-locations=classpath:static/,file:static/
    
    以jar包发布springboot项目时,默认会先使用jar包跟目录下的application.properties来作为项目配置文件。
    
    如果在不同的目录中存在多个配置文件,它的读取顺序是:
    
            1、config/application.properties(项目根目录中config目录下)
    
            2、config/application.yml
    
            3、application.properties(项目根目录下)
    
            4、application.yml
    
            5、resources/config/application.properties(项目resources目录中config目录下)
    
            6、resources/config/application.yml
    
            7、resources/application.properties(项目的resources目录下)
    
            8、resources/application.yml
    
    注:
    
         1、如果同一个目录下,有application.yml也有application.properties,默认先读取application.properties。
    
         2、如果同一个配置属性,在多个配置文件都配置了,默认使用第1个读取到的,后面读取的不覆盖前面读取到的。
    
         3、创建SpringBoot项目时,一般的配置文件放置在“项目的resources目录下”
  • 相关阅读:
    python 数据类型
    python核心语法
    python 基础知识
    format 用法
    有关python 函数参数
    模拟,队列与堆栈
    字符编码
    [LeetCode] Set Matrix Zeroes
    [LeetCode] Rotate Image
    [LeetCode] Unique Paths
  • 原文地址:https://www.cnblogs.com/lshan/p/13405288.html
Copyright © 2011-2022 走看看