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


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


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM