温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

SpringBoot打成jar运行后无法读取resources里的文件怎么办

发布时间:2020-08-19 10:05:35 来源:亿速云 阅读:1597 作者:小新 栏目:开发技术

这篇文章给大家分享的是有关SpringBoot打成jar运行后无法读取resources里的文件怎么办的内容。小编觉得挺实用的,因此分享给大家做个参考。一起跟随小编过来看看吧。

开发一个word替换功能时,因替换其中的内容功能需要 word 模版,就把 word_replace_tpl.docx 模版文件放到 resources 下

SpringBoot打成jar运行后无法读取resources里的文件怎么办

在开发环境中通过下面方法能读取word_replace_tpl.docx文件,但是打成jar包在 linux下运行后无法找到文件了

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

在开发环境运行时,会把资源文件编译到 项目\target\classes\static\office_template\xxx.docx 目录下,但是打包成jar后,

Resource下的文件是存在于jar这个文件里面,在磁盘上是没有真实路径存在的,它是位于jar内部的一个路径。所以通过ResourceUtils.getFile或者this.getClass().getResource("")方法无法正确获取文件。

我们用压缩软件打开 jar 文件,看看该word模版位于jar内部的路径在这里插入图片描述

SpringBoot打成jar运行后无法读取resources里的文件怎么办

怎么解决

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>

再次发布项目,访问功能,测试后已经在服务器上能读取模版文件并生成出新文件了

补充知识:两个list高效取出其中新增和相同的数

两个list循环,尽量避免双层循环以及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 ,更新的数据
      if (map.containsKey(j)){
        map.put(j,1);
      }else {
        //value为2 ,新增的数据
        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里的文件怎么办就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到吧!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI