解决springboot读取jar包中文件的问题
转载自:
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包中文件的问题的更多相关文章
- Jar中的Java程序如何读取Jar包中的资源文件
Jar中的Java程序如何读取Jar包中的资源文件 比如项目的组织结构如下(以idea中的项目为例): |-ProjectName |-.idea/ //这个目录是idea中项目的属性文件夹 |-s ...
- 读取Jar包中的资源问题探究
最近在写一个可执行jar的程序,程序中包含了2个资源包,一个是images,一个是files.问题来了,在Eclipse里开发的时候,当用File类来获取files下面的文件时,没有任何问题.但是当程 ...
- java读取jar包中的文件
随手写了一个java小工具,maven打包成功后,发现工具总是读不到打在jar包中的文件信息,要读取的文件位于 /src/main/resources 目录下,打包成功后,文件就在jar包中根目录下, ...
- 深入Jar包:Gradle构建可执行jar包与访问jar包中文件夹与文件
前言 Java的跨平台功能听起来很诱人可口,号称"Write Once,Run Everywhere",实际上是"Run Once,Debug Everywh" ...
- Java如何获取当前的jar包路径以及如何读取jar包中的资源
写作业的时候要输出一个record.dat文件到jar包的同级目录,但是不知道怎么定位jar包的路径.百度到的方法不很靠谱,所以在这里记录一下. 一:使用类路径 String path = this. ...
- springboot jar启动 读取jar包中相对路径文件报错
jar包启动后读取相对路径文件报异常: Caused by: java.io.FileNotFoundException: class path resource [***.***] cannot b ...
- SpringBoot打jar包-下载文件时报错-class path resource xxxxxx cannot be resolved to URL because it does not exist
一.问题由来 新项目的开发中,打包方式由war包改为了jar包的方式,这样在部署的时候更加的方便.测试环境使用pm2这个工具来管理项目的运行,停止,重启等等非常方便. 可是当测试人员在测试项目中的文件 ...
- 解决SpringBoot jar包中的文件读取问题
前言 SpringBoot微服务已成为业界主流,从开发到部署都非常省时省力,但是最近小明开发时遇到一个问题:在代码中读取资源文件(比如word文档.导出模版等),本地开发时可以正常读取 ,但是,当我们 ...
- java 从jar包中读取资源文件
在代码中读取一些资源文件(比如图片,音乐,文本等等),在集成环境(Eclipse)中运行的时候没有问题.但当打包成一个可执行的jar包(将资源文件一并打包)以后,这些资源文件找不到,如下代码: Jav ...
随机推荐
- EM13C添加agent记录两个报错
错误一:ADF_FACES-60097:For more information, please see the server's error log for an entry beginning w ...
- 'Specifying a namespace in include() without providing an app_name '
'Specifying a namespace in include() without providing an app_name ’ 从include()函数可以看出来,这个函数有两个参数,一个a ...
- manjaro安装后的配置
1.添加中科大源 参考https://blog.csdn.net/liyunfu233/article/details/87381017 sudo nano /etc/pacman.conf 在文末添 ...
- 百度开源的分布式唯一ID生成器UidGenerator,解决了时钟回拨问题
UidGenerator是百度开源的Java语言实现,基于Snowflake算法的唯一ID生成器.而且,它非常适合虚拟环境,比如:Docker.另外,它通过消费未来时间克服了雪花算法的并发限制.Uid ...
- vue组件定义方式,vue父子组件间的传值
vue组件定义方式,vue父子组件间的传值 <!DOCTYPE html> <html lang="zh-cn"> <head> <met ...
- python3中的数字类型
今天在学校机房刷python题时发现自己对python中的数字类型不理解,回寝室后百度一下. 现在做一个总结. python中的数字类型有: 整数,布尔值,复数,科学计数法,浮点数 1,整数,大小没有 ...
- 数据库——SQL-SERVER练习(6) 数据库安全性
一.实验准备 (1)运行SQL-SERVER服务管理器, 启动服务(2)运行查询分析器, 以DBA身份登录数据库服务器: 用户名sa, 密码123456(3)打开CREATE-TABLE ...
- tensorflow查看使用的是cpu还是gpu
https://stackoverflow.com/questions/38009682/how-to-tell-if-tensorflow-is-using-gpu-acceleration-fro ...
- [debug] 解决在C++编写过程中的“找到一个或多个多重定义的符号”
如下图: 其在 common.h 中定义了一个变量a ,然后在两个 cpp 文件中都是用它. 在这种情况下,链接时就会出现 “找到一个或多个多重定义的符号”. 解决方案: 在某个cpp文件中定义,然后 ...
- MongoDB 修改数据Cannot change the size of a document in a capped collection: * != *"
MongoDB修改数据库数据的时候报错 原因: 集合被设置成了 固定集合 .固定集合的数据不能被修改.只能查找-删除-再插入,也就是创建集合的时候设置了capped参数为true 解决: 创建集合的时 ...