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