解決SpringBoot打成jar運行后無法讀取resources里的文件問題
開發(fā)一個word替換功能時,因替換其中的內容功能需要 word 模版,就把 word_replace_tpl.docx 模版文件放到 resources 下

在開發(fā)環(huán)境中通過下面方法能讀取word_replace_tpl.docx文件,但是打成jar包在 linux下運行后無法找到文件了
File file = ResourceUtils.getFile(ResourceUtils.CLASSPATH_URL_PREFIX + "static/office_template/xxx.docx");
在開發(fā)環(huán)境運行時,會把資源文件編譯到 項目\target\classes\static\office_template\xxx.docx 目錄下,但是打包成jar后,
Resource下的文件是存在于jar這個文件里面,在磁盤上是沒有真實路徑存在的,它是位于jar內部的一個路徑。所以通過ResourceUtils.getFile或者this.getClass().getResource("")方法無法正確獲取文件。
我們用壓縮軟件打開 jar 文件,看看該word模版位于jar內部的路徑在這里插入圖片描述

怎么解決
1.把該模版文件放到jar項目外,在項目中配置該模版文件的絕對路徑,不太推薦這種方式,可能會忘記配置模版
2.通過 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();
還要在項目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ā)布項目,訪問功能,測試后已經在服務器上能讀取模版文件并生成出新文件了
補充知識:兩個list高效取出其中新增和相同的數(shù)
兩個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();
}
以上這篇解決SpringBoot打成jar運行后無法讀取resources里的文件問題就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
java中將科學計數(shù)法轉換普通計數(shù)法的簡單方法
下面小編就為大家?guī)硪黄猨ava中將科學計數(shù)法轉換普通計數(shù)法的簡單方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-12-12
詳解SpringBoot修改啟動端口server.port的四種方式
這篇文章主要介紹了詳解SpringBoot修改啟動端口server.port的四種方式,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-07-07
Spring Boot 集成 Kafkad的實現(xiàn)示例
這篇文章主要介紹了Spring Boot 集成 Kafkad的示例,幫助大家更好的理解和學習使用Spring Boot框架,感興趣的朋友可以了解下2021-04-04
淺談Java中replace與replaceAll區(qū)別
這篇文章主要介紹了Java中replace與replaceAll區(qū)別,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-03-03

