springboot 項目絕對路徑

File path = null;
try {
   path = new File(ResourceUtils.getURL("classpath:").getPath());
} catch (FileNotFoundException e) {

}
if (path == null || !path.exists()) {
   path = new File("");
}
String pathStr = path.getAbsolutePath();
// 如果是在eclipse中運行,則和target同級目錄,如果是jar部署到服務器,則默認和jar包同級
pathStr = pathStr.replace("/target/classes", "/upload");

File upload = new File(pathStr);
if (!upload.exists()) {
   upload.mkdirs();
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章