您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關(guān)SpringBoot打成jar運(yùn)行后無(wú)法讀取resources里的文件怎么辦的內(nèi)容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧。
開(kāi)發(fā)一個(gè)word替換功能時(shí),因替換其中的內(nèi)容功能需要 word 模版,就把 word_replace_tpl.docx 模版文件放到 resources 下
在開(kāi)發(fā)環(huán)境中通過(guò)下面方法能讀取word_replace_tpl.docx文件,但是打成jar包在 linux下運(yùn)行后無(wú)法找到文件了
File file = ResourceUtils.getFile(ResourceUtils.CLASSPATH_URL_PREFIX + "static/office_template/xxx.docx");
在開(kāi)發(fā)環(huán)境運(yùn)行時(shí),會(huì)把資源文件編譯到 項(xiàng)目\target\classes\static\office_template\xxx.docx 目錄下,但是打包成jar后,
Resource下的文件是存在于jar這個(gè)文件里面,在磁盤(pán)上是沒(méi)有真實(shí)路徑存在的,它是位于jar內(nèi)部的一個(gè)路徑。所以通過(guò)ResourceUtils.getFile或者this.getClass().getResource("")方法無(wú)法正確獲取文件。
我們用壓縮軟件打開(kāi) jar 文件,看看該word模版位于jar內(nèi)部的路徑在這里插入圖片描述
怎么解決
1.把該模版文件放到j(luò)ar項(xiàng)目外,在項(xiàng)目中配置該模版文件的絕對(duì)路徑,不太推薦這種方式,可能會(huì)忘記配置模版
2.通過(guò) ClassPathResource resource = new ClassPathResource(“static/office_template/word_replace_tpl.docx”);方式讀取
用第二種方式讀取jar中的文件流
ClassPathResource resource = new ClassPathResource("static/office_template/word_replace_tpl.docx"); File sourceFile = resource.getFile(); InputStream fis = resource.getInputStream();
還要在項(xiàng)目pom.xml中配置resources情況
<build> <!-- 定義包含這些資源文件,能在jar包中獲取這些文件 --> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> <include>**/*.yml</include> </includes> <!--是否替換資源中的屬性--> <filtering>false</filtering> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.*</include> </includes> <!--是否替換資源中的屬性--> <filtering>false</filtering> </resource> </resources> </build>
再次發(fā)布項(xiàng)目,訪問(wèn)功能,測(cè)試后已經(jīng)在服務(wù)器上能讀取模版文件并生成出新文件了
補(bǔ)充知識(shí):兩個(gè)list高效取出其中新增和相同的數(shù)
兩個(gè)list循環(huán),盡量避免雙層循環(huán)以及contains的使用
public static void test(){ List<Integer> oldList = new ArrayList<Integer>(){{add(1);add(2);add(4);add(5);}}; List<Integer> newList = new ArrayList<Integer>(){{add(3);add(4);add(5);add(6);}}; Map<Integer,Integer> map = new HashMap<>(); for (Integer i: oldList ) { map.put(i,0); } System.out.print(map); for (Integer j: newList ) { //value為1 ,更新的數(shù)據(jù) if (map.containsKey(j)){ map.put(j,1); }else { //value為2 ,新增的數(shù)據(jù) map.put(j,2); } } System.out.println(map); for (Map.Entry<Integer,Integer> entry: map.entrySet() ) { if(entry.getValue().equals(0)){ System.out.println("舊的值:"+entry.getKey()); } if(entry.getValue().equals(1)){ System.out.println("更新的值:"+entry.getKey()); } if(entry.getValue().equals(3)){ System.out.println("新增的值:"+entry.getKey()); } } System.out.println(map); } public static void main(String[] arg){ test(); }
感謝各位的閱讀!關(guān)于SpringBoot打成jar運(yùn)行后無(wú)法讀取resources里的文件怎么辦就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。