springboot獲取項目目錄路徑的方法
本文講解"springboot獲取項目目錄路徑的方法",希望能夠解決相關(guān)問題。
目錄- springboot獲取項目目錄路徑
- springboot獲取resources目錄資源文件9種方式
- 方式一
- 方式二
- 方式三
- 方式四(重要)
- 方式五(重要)
- 方式六(重要)
- 方式七
- 方式八
- 方式九
- 總結(jié)
springboot獲取項目目錄路徑
springboot部署后獲取項目的路徑
//獲取跟目錄(絕對路徑) 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()); //在開發(fā)測試模式時,得到的地址為:{項目跟目錄}/target/static/images/upload/ //在打包成jar正式發(fā)布時,得到的地址為:{發(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方法,直接通過getresource(filename)方法獲取文件路徑,注意如果是路徑中帶有中文一定要使用urldecoder.decode解碼。
/** ?*?直接通過文件名getpath來獲取路徑 ?* ?*?@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");//如果路徑中帶有中文會被urlencoder,因此這里需要解碼 ????system.out.println(filepath); ????getfilecontent(filepath); }
方式三
直接通過文件名+getfile()來獲取文件。如果是文件路徑的話getfile和getpath效果是一樣的,如果是url路徑的話getpath是帶有參數(shù)的路徑。
如下所示:
url.getfile()=/pub/files/foobar.txt?id=123456 url.getpath()=/pub/files/foobar.txt
使用getfile()方式獲取文件的代碼如下:
/** ?*?直接通過文件名+getfile()來獲取 ?* ?*?@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");//如果路徑中帶有中文會被urlencoder,因此這里需要解碼 ????system.out.println(filepath); ????getfilecontent(filepath); }
方式四(重要)
直接使用getresourceasstream方法獲取流,上面的幾種方式都需要獲取文件路徑,但是在springboot中所有文件都在jar包中,沒有一個實際的路徑,因此可以使用以下方式。
/** ?*?直接使用getresourceasstream方法獲取流 ?*?springboot項目中需要使用此種方法,因為jar包中沒有一個實際的路徑存放文件 ?* ?*?@param?filename ?*?@throws?ioexception ?*/ public?void?function4(string?filename)?throws?ioexception?{ ????inputstream?in?=?this.getclass().getclassloader().getresourceasstream(filename); ????getfilecontent(in); }
方式五(重要)
主要也是使用getresourceasstream方法獲取流,不使用getclassloader可以使用getresourceasstream("/配置測試.txt")直接從resources根路徑下獲取,springboot中所有文件都在jar包中,沒有一個實際的路徑,因此可以使用以下方式。
/** ?*?直接使用getresourceasstream方法獲取流 ?*?如果不使用getclassloader,可以使用getresourceasstream("/配置測試.txt")直接從resources根路徑下獲取 ?* ?*?@param?filename ?*?@throws?ioexception ?*/ public?void?function5(string?filename)?throws?ioexception?{ ????inputstream?in?=?this.getclass().getresourceasstream("/"?+?filename); ????getfilecontent(in); }
方式六(重要)
通過classpathresource類獲取文件流,springboot中所有文件都在jar包中,沒有一個實際的路徑,因此可以使用以下方式。
/** ?*?通過classpathresource類獲取,建議springboot中使用 ?*?springboot項目中需要使用此種方法,因為jar包中沒有一個實際的路徑存放文件 ?* ?*?@param?filename ?*?@throws?ioexception ?*/ public?void?function6(string?filename)?throws?ioexception?{ ????classpathresource?classpathresource?=?new?classpathresource(filename); ????inputstream?inputstream?=?classpathresource.getinputstream(); ????getfilecontent(inputstream); }
方式七
通過絕對路徑獲取項目中文件的位置,只是本地絕對路徑,不能用于服務器獲取。
/** ?*?通過絕對路徑獲取項目中文件的位置(不能用于服務器) ?*?@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); }
方式八
通過new file("")獲取當前的絕對路徑,只是本地絕對路徑,不能用于服務器獲取。
/** ?*?通過絕對路徑獲取項目中文件的位置(不能用于服務器) ?*?@param?filename ?*?@throws?ioexception ?*/ public?void?function8(string?filename)?throws?ioexception?{ ????//參數(shù)為空 ????file?directory?=?new?file(""); ????//規(guī)范路徑:getcanonicalpath()?方法返回絕對路徑,會把?..\?、.\?這樣的符號解析掉 ????string?rootcanonicalpath?=?directory.getcanonicalpath(); ????//絕對路徑:getabsolutepath()?方法返回文件的絕對路徑,如果構(gòu)造的時候是全路徑就直接返回全路徑,如果構(gòu)造時是相對路徑,就返回當前目錄的路徑?+?構(gòu)造?file?對象時的路徑 ????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); }
方式九
主要是通過設(shè)置環(huán)境變量,將文件放在環(huán)境變量中,原理也是通過絕對路徑獲取。
示例中我設(shè)置了一個環(huán)境變量:test_root=e:\\workspace\\git\\spring-framework-learning-example
system.getenv("test_root"); system.getproperty("test_root")
通過設(shè)置環(huán)境變量的方式,然后通過絕對路徑獲取文件
/** ?*?通過絕對路徑獲取項目中文件的位置 ?* ?*?@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); }
關(guān)于 "springboot獲取項目目錄路徑的方法" 就介紹到此。希望多多支持碩編程。