springboot之如何獲取項(xiàng)目目錄路徑
springboot獲取項(xiàng)目目錄路徑
springboot部署后獲取項(xiàng)目的路徑
//獲取跟目錄(絕對(duì)路徑)
File path = new File(ResourceUtils.getURL("classpath:").getPath());
if(!path.exists()) path = new File("");
System.out.println("path:"+path.getAbsolutePath());
//如果上傳目錄為/static/images/upload/,則可以如下獲?。?
File upload = new File(path.getAbsolutePath(),"static/images/upload/");
if(!upload.exists()) upload.mkdirs();
System.out.println("upload url:"+upload.getAbsolutePath());
//在開(kāi)發(fā)測(cè)試模式時(shí),得到的地址為:{項(xiàng)目跟目錄}/target/static/images/upload/
//在打包成jar正式發(fā)布時(shí),得到的地址為:{發(fā)布jar包目錄}/static/images/upload/springboot獲取resources目錄資源文件9種方式

本文中提供了九種方式獲取resources目錄下文件的方式。
其中打印文件的方法如下:
/**
* 根據(jù)文件路徑讀取文件內(nèi)容
*
* @param fileInPath
* @throws IOException
*/
public static void getFileContent(Object fileInPath) throws IOException {
BufferedReader br = null;
if (fileInPath == null) {
return;
}
if (fileInPath instanceof String) {
br = new BufferedReader(new FileReader(new File((String) fileInPath)));
} else if (fileInPath instanceof InputStream) {
br = new BufferedReader(new InputStreamReader((InputStream) fileInPath));
}
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
}方式一
主要核心方法是使用getResource和getPath方法,這里的getResource("")里面是空字符串
public void function1(String fileName) throws IOException {
String path = this.getClass().getClassLoader().getResource("").getPath();//注意getResource("")里面是空字符串
System.out.println(path);
String filePath = path + fileName;
System.out.println(filePath);
getFileContent(filePath);
}方式二
主要核心方法是使用getResource和getPath方法,直接通過(guò)getResource(fileName)方法獲取文件路徑,注意如果是路徑中帶有中文一定要使用URLDecoder.decode解碼。
/**
* 直接通過(guò)文件名getPath來(lái)獲取路徑
*
* @param fileName
* @throws IOException
*/
public void function2(String fileName) throws IOException {
String path = this.getClass().getClassLoader().getResource(fileName).getPath();//注意getResource("")里面是空字符串
System.out.println(path);
String filePath = URLDecoder.decode(path, "UTF-8");//如果路徑中帶有中文會(huì)被URLEncoder,因此這里需要解碼
System.out.println(filePath);
getFileContent(filePath);
}方式三
直接通過(guò)文件名+getFile()來(lái)獲取文件。如果是文件路徑的話getFile和getPath效果是一樣的,如果是URL路徑的話getPath是帶有參數(shù)的路徑。
如下所示:
url.getFile()=/pub/files/foobar.txt?id=123456 url.getPath()=/pub/files/foobar.txt
使用getFile()方式獲取文件的代碼如下:
/**
* 直接通過(guò)文件名+getFile()來(lái)獲取
*
* @param fileName
* @throws IOException
*/
public void function3(String fileName) throws IOException {
String path = this.getClass().getClassLoader().getResource(fileName).getFile();//注意getResource("")里面是空字符串
System.out.println(path);
String filePath = URLDecoder.decode(path, "UTF-8");//如果路徑中帶有中文會(huì)被URLEncoder,因此這里需要解碼
System.out.println(filePath);
getFileContent(filePath);
}方式四(重要)
直接使用getResourceAsStream方法獲取流,上面的幾種方式都需要獲取文件路徑,但是在SpringBoot中所有文件都在jar包中,沒(méi)有一個(gè)實(shí)際的路徑,因此可以使用以下方式。
/**
* 直接使用getResourceAsStream方法獲取流
* springboot項(xiàng)目中需要使用此種方法,因?yàn)閖ar包中沒(méi)有一個(gè)實(shí)際的路徑存放文件
*
* @param fileName
* @throws IOException
*/
public void function4(String fileName) throws IOException {
InputStream in = this.getClass().getClassLoader().getResourceAsStream(fileName);
getFileContent(in);
}方式五(重要)
主要也是使用getResourceAsStream方法獲取流,不使用getClassLoader可以使用getResourceAsStream("/配置測(cè)試.txt")直接從resources根路徑下獲取,SpringBoot中所有文件都在jar包中,沒(méi)有一個(gè)實(shí)際的路徑,因此可以使用以下方式。
/**
* 直接使用getResourceAsStream方法獲取流
* 如果不使用getClassLoader,可以使用getResourceAsStream("/配置測(cè)試.txt")直接從resources根路徑下獲取
*
* @param fileName
* @throws IOException
*/
public void function5(String fileName) throws IOException {
InputStream in = this.getClass().getResourceAsStream("/" + fileName);
getFileContent(in);
}方式六(重要)
通過(guò)ClassPathResource類(lèi)獲取文件流,SpringBoot中所有文件都在jar包中,沒(méi)有一個(gè)實(shí)際的路徑,因此可以使用以下方式。
/**
* 通過(guò)ClassPathResource類(lèi)獲取,建議SpringBoot中使用
* springboot項(xiàng)目中需要使用此種方法,因?yàn)閖ar包中沒(méi)有一個(gè)實(shí)際的路徑存放文件
*
* @param fileName
* @throws IOException
*/
public void function6(String fileName) throws IOException {
ClassPathResource classPathResource = new ClassPathResource(fileName);
InputStream inputStream = classPathResource.getInputStream();
getFileContent(inputStream);
}方式七
通過(guò)絕對(duì)路徑獲取項(xiàng)目中文件的位置,只是本地絕對(duì)路徑,不能用于服務(wù)器獲取。
/**
* 通過(guò)絕對(duì)路徑獲取項(xiàng)目中文件的位置(不能用于服務(wù)器)
* @param fileName
* @throws IOException
*/
public void function7(String fileName) throws IOException {
String rootPath = System.getProperty("user.dir");//E:\WorkSpace\Git\spring-framework-learning-example
String filePath = rootPath + "\\chapter-2-springmvc-quickstart\\src\\main\\resources\\"+fileName;
getFileContent(filePath);
}方式八
通過(guò)new File("")獲取當(dāng)前的絕對(duì)路徑,只是本地絕對(duì)路徑,不能用于服務(wù)器獲取。
/**
* 通過(guò)絕對(duì)路徑獲取項(xiàng)目中文件的位置(不能用于服務(wù)器)
* @param fileName
* @throws IOException
*/
public void function8(String fileName) throws IOException {
//參數(shù)為空
File directory = new File("");
//規(guī)范路徑:getCanonicalPath() 方法返回絕對(duì)路徑,會(huì)把 ..\ 、.\ 這樣的符號(hào)解析掉
String rootCanonicalPath = directory.getCanonicalPath();
//絕對(duì)路徑:getAbsolutePath() 方法返回文件的絕對(duì)路徑,如果構(gòu)造的時(shí)候是全路徑就直接返回全路徑,如果構(gòu)造時(shí)是相對(duì)路徑,就返回當(dāng)前目錄的路徑 + 構(gòu)造 File 對(duì)象時(shí)的路徑
String rootAbsolutePath =directory.getAbsolutePath();
System.out.println(rootCanonicalPath);
System.out.println(rootAbsolutePath);
String filePath = rootCanonicalPath + "\\chapter-2-springmvc-quickstart\\src\\main\\resources\\"+fileName;
getFileContent(filePath);
}方式九
主要是通過(guò)設(shè)置環(huán)境變量,將文件放在環(huán)境變量中,原理也是通過(guò)絕對(duì)路徑獲取。
示例中我設(shè)置了一個(gè)環(huán)境變量:TEST_ROOT=E:\\WorkSpace\\Git\\spring-framework-learning-example
System.getenv("TEST_ROOT");
System.getProperty("TEST_ROOT")通過(guò)設(shè)置環(huán)境變量的方式,然后通過(guò)絕對(duì)路徑獲取文件
/**
* 通過(guò)絕對(duì)路徑獲取項(xiàng)目中文件的位置
*
* @param fileName
* @throws IOException
*/
public void function9(String fileName) throws IOException {
System.setProperty("TEST_ROOT","E:\\WorkSpace\\Git\\spring-framework-learning-example");
//參數(shù)為空
String rootPath = System.getProperty("TEST_ROOT");
System.out.println(rootPath);
String filePath = rootPath + "\\chapter-2-springmvc-quickstart\\src\\main\\resources\\"+fileName;
getFileContent(filePath);
}總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Mybatis使用foreach標(biāo)簽實(shí)現(xiàn)批量插入方式
這篇文章主要介紹了Mybatis使用foreach標(biāo)簽實(shí)現(xiàn)批量插入方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03
JAVA中通過(guò)自定義注解進(jìn)行數(shù)據(jù)驗(yàn)證的方法
java 自定義注解驗(yàn)證可自己添加所需要的注解,下面這篇文章主要給大家介紹了關(guān)于JAVA中通過(guò)自定義注解進(jìn)行數(shù)據(jù)驗(yàn)證的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-08-08
Spring4下validation數(shù)據(jù)校驗(yàn)無(wú)效(maven)的解決
這篇文章主要介紹了Spring4下validation數(shù)據(jù)校驗(yàn)無(wú)效(maven)的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06
Springboot實(shí)現(xiàn)緩存預(yù)熱的方法
在系統(tǒng)啟動(dòng)之前通過(guò)預(yù)先將常用數(shù)據(jù)加載到緩存中,以提高緩存命中率和系統(tǒng)性能的過(guò)程,緩存預(yù)熱的目的是盡可能地避免緩存擊穿和緩存雪崩,這篇文章主要介紹了Springboot實(shí)現(xiàn)緩存預(yù)熱,需要的朋友可以參考下2024-03-03
windows 部署JAVA環(huán)境安裝iDea的詳細(xì)步驟
這篇文章主要介紹了windows 部署JAVA環(huán)境安裝iDea的詳細(xì)步驟,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08
java中vector與hashtable操作實(shí)例分享
java中vector與hashtable操作實(shí)例,有需要的朋友可以參考一下2014-01-01
使用Filter實(shí)現(xiàn)登錄權(quán)限驗(yàn)證
這篇文章主要為大家詳細(xì)介紹了使用Filter實(shí)現(xiàn)登錄權(quán)限驗(yàn)證,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-10-10
Java Web項(xiàng)目中實(shí)現(xiàn)文件下載功能的實(shí)例教程
這篇文章主要介紹了Java Web項(xiàng)目中實(shí)現(xiàn)文件下載功能的實(shí)例教程,分別講解了通過(guò)超鏈接實(shí)現(xiàn)下載以及通過(guò)Servlet程序?qū)崿F(xiàn)下載的方式,需要的朋友可以參考下2016-05-05
Java位運(yùn)算和邏輯運(yùn)算的區(qū)別實(shí)例
Java位運(yùn)算和邏輯運(yùn)算的區(qū)別實(shí)例,請(qǐng)參考下面代碼,希望對(duì)你有所幫助2013-02-02

