国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - 解決SpringBoot打成jar運(yùn)行后無法讀取resources里的文件問題

解決SpringBoot打成jar運(yùn)行后無法讀取resources里的文件問題

2020-08-19 00:18閃耀的瞬間 Java教程

這篇文章主要介紹了解決SpringBoot打成jar運(yùn)行后無法讀取resources里的文件問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

開發(fā)一個word替換功能時,因替換其中的內(nèi)容功能需要 word 模版,就把 word_replace_tpl.docx 模版文件放到 resources

解決SpringBoot打成jar運(yùn)行后無法讀取resources里的文件問題

在開發(fā)環(huán)境中通過下面方法能讀取word_replace_tpl.docx文件,但是打成jar包在 linux下運(yùn)行后無法找到文件了

File file = ResourceUtils.getFile(ResourceUtils.CLASSPATH_URL_PREFIX + "static/office_template/xxx.docx");

在開發(fā)環(huán)境運(yùn)行時,會把資源文件編譯到 項(xiàng)目\target\classes\static\office_template\xxx.docx 目錄下,但是打包成jar后,

Resource下的文件是存在于jar這個文件里面,在磁盤上是沒有真實(shí)路徑存在的,它是位于jar內(nèi)部的一個路徑。所以通過ResourceUtils.getFile或者this.getClass().getResource("")方法無法正確獲取文件。

我們用壓縮軟件打開 jar 文件,看看該word模版位于jar內(nèi)部的路徑在這里插入圖片描述

解決SpringBoot打成jar運(yùn)行后無法讀取resources里的文件問題

怎么解決

1.把該模版文件放到j(luò)ar項(xiàng)目外,在項(xiàng)目中配置該模版文件的絕對路徑,不太推薦這種方式,可能會忘記配置模版

2.通過 ClassPathResource resource = new ClassPathResource(“static/office_template/word_replace_tpl.docx”);方式讀取

用第二種方式讀取jar中的文件流

?
1
2
3
ClassPathResource resource = new ClassPathResource("static/office_template/word_replace_tpl.docx");
File sourceFile = resource.getFile();
InputStream fis = resource.getInputStream();

還要在項(xiàng)目pom.xml中配置resources情況

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<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)目,訪問功能,測試后已經(jīng)在服務(wù)器上能讀取模版文件并生成出新文件了

補(bǔ)充知識:兩個list高效取出其中新增和相同的數(shù)

兩個list循環(huán),盡量避免雙層循環(huán)以及contains的使用

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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運(yùn)行后無法讀取resources里的文件問題就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持服務(wù)器之家。

原文鏈接:https://blog.csdn.net/zhuyu19911016520/article/details/98071242

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 亚洲精选一区二区 | 色视频在线免费观看 | 精品在线一区 | 日韩成人在线网站 | 日本精品在线观看视频 | 这里只有精品国产 | 九色在线 | 久久久久久成人 | 夜夜操操操操 | 久久久久一区 | 亚洲视频一区在线播放 | 久久久精品网站 | 国产精品高潮呻吟久久 | 免费观看视频毛片 | 777色狠狠一区二区三区 | 国产精品123区 | 精品久久电影 | 国产精品a久久 | 国产三区av| 最近2019中文字幕大全视频10 | 亚洲精品乱码久久久久久金桔影视 | 精品国产黄a∨片高清在线 欧美一级免费 | 日韩欧美视频免费观看 | 欧美黄视频| 日韩欧美视频免费 | 日韩成人一区 | 日韩精品一区二区三区在线 | 国产精品毛片无码 | 国产精品一二三区视频 | 精品福利一区二区三区免费视频 | 婷婷成人基地 | 亚洲精品www久久久久久广东 | 一区二区精品在线 | 乱人伦xxxx国语对白 | 婷婷综合网 | 亚洲精品国产一区 | 一级片在线观看 | 免费观看av | 欧美综合激情 | www.色婷婷 | 午夜影院在线 |