springboo在开发和发布项目时的路径问题总结如下:
1、springboot获取当前项目路径的地址
System.getProperty("user.dir")
输出目录: G:\outshine\wangsoso
2、获取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配置文件中设置静态资源路径,如下所示:
#设置