首页 > 编程语言 > 详细

Spring Boot 获取项目根路径或者资源文件的路径

时间:2020-05-06 12:50:58      阅读:82      评论:0      收藏:0      [点我收藏+]

https://blog.csdn.net/weixin_40584261/article/details/88424058

在Spring Boot项目中,有时候需要获取项目的根路径,可以通过以下方法获取:

/**
* 获取项目根路径
*
* @return
*/
private static String getResourceBasePath() {
// 获取跟目录
File path = null;
try {
path = new File(ResourceUtils.getURL("classpath:").getPath());
} catch (FileNotFoundException e) {
// nothing to do
}
if (path == null || !path.exists()) {
path = new File("");
}

String pathStr = path.getAbsolutePath();
// 如果是在eclipse中运行,则和target同级目录,如果是jar部署到服务器,则默认和jar包同级
pathStr = pathStr.replace("\\target\\classes", "");

return pathStr;
}
}
————————————————
版权声明:本文为CSDN博主「谦谦公子爱编程」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_40584261/article/details/88424058

Spring Boot 获取项目根路径或者资源文件的路径

原文:https://www.cnblogs.com/linus-tan/p/12835401.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!