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