转载自:

https://www.oschina.net/question/2272552_2269641

https://stackoverflow.com/questions/25869428/classpath-resource-not-found-when-running-as-jar

几个实现方式:

String data = "";
ClassPathResource cpr = new ClassPathResource("static/file.txt");
try {
byte[] bdata = FileCopyUtils.copyToByteArray(cpr.getInputStream());
data = new String(bdata, StandardCharsets.UTF_8);
} catch (IOException e) {
LOG.warn("IOException", e);
}

  

场景二:

ClassPathResource classPathResource = new ClassPathResource("static/something.txt");

InputStream inputStream = classPathResource.getInputStream();
File somethingFile = File.createTempFile("test", ".txt");
try {
FileUtils.copyInputStreamToFile(inputStream, somethingFile);
} finally {
IOUtils.closeQuietly(inputStream);
}

  

场景三:

Resource[] resources;

        try {
resources = ResourcePatternUtils.getResourcePatternResolver(resourceLoader).getResources("classpath:" + location + "/*.json");
for (int i = 0; i < resources.length; i++) {
try {
InputStream is = resources[i].getInputStream();
byte[] encoded = IOUtils.toByteArray(is);
String content = new String(encoded, Charset.forName("UTF-8"));
}
}
} catch (IOException e) {
throw new UncheckedIOException(e);
}

转载自:

https://blog.csdn.net/zhuyu19911016520/article/details/79060389

在开发环境中,使用 ResourceUtils.getFile(“classpath:static/files/8k.wav”) 能读取到 File ,结果打包发布后,读取不了。

解决方法

InputStream stream = getClass().getClassLoader().getResourceAsStream("static/files/8k.wav");
File targetFile = new File("files/8k.wav");
FileUtils.copyInputStreamToFile(stream, targetFile);

或通过以下方式获取

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

参照此博客中的方法:https://blog.csdn.net/zhuyu19911016520/article/details/98071242

开发一个word替换功能时,因替换其中的内容功能需要 word 模版,就把 word_replace_tpl.docx 模版文件放到 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内部的路径

怎么解决

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>

  

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

解决springboot读取jar包中文件的问题的更多相关文章

  1. Jar中的Java程序如何读取Jar包中的资源文件

    Jar中的Java程序如何读取Jar包中的资源文件 比如项目的组织结构如下(以idea中的项目为例): |-ProjectName |-.idea/  //这个目录是idea中项目的属性文件夹 |-s ...

  2. 读取Jar包中的资源问题探究

    最近在写一个可执行jar的程序,程序中包含了2个资源包,一个是images,一个是files.问题来了,在Eclipse里开发的时候,当用File类来获取files下面的文件时,没有任何问题.但是当程 ...

  3. java读取jar包中的文件

    随手写了一个java小工具,maven打包成功后,发现工具总是读不到打在jar包中的文件信息,要读取的文件位于 /src/main/resources 目录下,打包成功后,文件就在jar包中根目录下, ...

  4. 深入Jar包:Gradle构建可执行jar包与访问jar包中文件夹与文件

    前言 Java的跨平台功能听起来很诱人可口,号称"Write Once,Run Everywhere",实际上是"Run Once,Debug Everywh" ...

  5. Java如何获取当前的jar包路径以及如何读取jar包中的资源

    写作业的时候要输出一个record.dat文件到jar包的同级目录,但是不知道怎么定位jar包的路径.百度到的方法不很靠谱,所以在这里记录一下. 一:使用类路径 String path = this. ...

  6. springboot jar启动 读取jar包中相对路径文件报错

    jar包启动后读取相对路径文件报异常: Caused by: java.io.FileNotFoundException: class path resource [***.***] cannot b ...

  7. SpringBoot打jar包-下载文件时报错-class path resource xxxxxx cannot be resolved to URL because it does not exist

    一.问题由来 新项目的开发中,打包方式由war包改为了jar包的方式,这样在部署的时候更加的方便.测试环境使用pm2这个工具来管理项目的运行,停止,重启等等非常方便. 可是当测试人员在测试项目中的文件 ...

  8. 解决SpringBoot jar包中的文件读取问题

    前言 SpringBoot微服务已成为业界主流,从开发到部署都非常省时省力,但是最近小明开发时遇到一个问题:在代码中读取资源文件(比如word文档.导出模版等),本地开发时可以正常读取 ,但是,当我们 ...

  9. java 从jar包中读取资源文件

    在代码中读取一些资源文件(比如图片,音乐,文本等等),在集成环境(Eclipse)中运行的时候没有问题.但当打包成一个可执行的jar包(将资源文件一并打包)以后,这些资源文件找不到,如下代码: Jav ...

随机推荐

  1. Redis主从复制的基本操作

    一,安装: 1.1.将redis压缩包放到 /opt 下. 2.解压 3.进入目录执行  make 4.执行  make  install 5.在 / 下创建redis文件夹mkdir redis 6 ...

  2. 以太网驱动的流程浅析(二)-Ifconfig的详细代码流程【原创】

    以太网驱动流程浅析(二)-ifconfig的详细代码流程 Author:张昺华 Email:920052390@qq.com Time:2019年3月23日星期六 此文也在我的个人公众号以及<L ...

  3. 2018CCPC吉林赛区

    传送门 A - The Fool 整除分块即可. B - The World 模拟即可. C - Justice 题意: 给出\(n\)个数\(k_i\),每个数的权值为\(\frac{1}{2^{k ...

  4. Centos7更新阿里云的yum源

    1.进入yum文件夹 cd /etc/yum.repos.d/ 2.下载阿里云源 wget "http://mirrors.aliyun.com/repo/Centos-7.repo&quo ...

  5. new String(request.getParameter("userID").trim().getBytes("8859_1"))的含义是什么?

    new String(request.getParameter("userID").trim().getBytes("8859_1")) request.get ...

  6. leetcode 752. 打开转盘锁

    地址 https://leetcode-cn.com/problems/open-the-lock/ 你有一个带有四个圆形拨轮的转盘锁.每个拨轮都有10个数字: '0', '1', '2', '3', ...

  7. poj 1979 Red and Black 题解《挑战程序设计竞赛》

    地址 http://poj.org/problem?id=1979 Description There is a rectangular room, covered with square tiles ...

  8. html和css常见问题解答

    1. 详细描述层叠和继承的概念. 元素内嵌样式(用元素的全局属性style定义的样式) 文档内嵌样式(定义在style元素中的样式) 外部样式(用link元素导入的样式) 用户样式(用户定义的样式) ...

  9. 《细说PHP》第四版 样章 第18章 数据库抽象层PDO 6

    18.5.3  PDO的错误处理模式 PDO共提供了3种不同的错误处理模式,不仅可以满足不同风格的编程,也可以调整扩展处理错误的方式. 1.PDO::ERRMODE_SILENT 这是默认模式,在错误 ...

  10. Fragment生命周期函数调用(ViewPager切换方式)

    在使用ViewPager时,Google亲爹为我们提供了多种PagerAdapter.其中,与Fragment相关的是FragmentPagerAdapter和FragmentStatePagerAd ...