本文主要是介绍系列二十九、Spring Boot打成jar包后,resources目录下的资源文件获取不到,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
一、Spring Boot打成jar包后,resources目录下的资源文件获取不到
1.1、问题描述
在做公司业务开发时,有一个地方是使用EasyExcel模板技术进行文件上传,测试环境是OK的,但是和前后端联调验证测试通过后,上传到生产环境就不行了,后来也是花费了很多时间才找出其中的问题,以下为解决方案。
1.2、pom里边添加如下配置
<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>
1.3、修改加载资源文件的方式
// 加载资源文件方式1:
ClassPathResource classPathResource = new ClassPathResource("static/budgetunit/employee_budget_unit_export.xlsx");
String templatePath = classPathResource.getPath();// 加载资源文件方式2(推荐):
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("static/budgetunit/employee_budget_unit_export.xlsx");
String templatePath = ClassLoader.getSystemResource("templates/budgetunit/employee_budget_unit_export.xlsx").getPath();
这篇关于系列二十九、Spring Boot打成jar包后,resources目录下的资源文件获取不到的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!