系列二十九、Spring Boot打成jar包后,resources目录下的资源文件获取不到

一、Spring Boot打成jar包后,resources目录下的资源文件获取不到

1.1、问题描述

在做公司业务开发时,有一个地方是使用EasyExcel模板技术进行文件上传,测试环境是OK的,但是和前后端联调验证测试通过后,上传到生产环境就不行了,后来也是花费了很多时间才找出其中的问题,以下为解决方案。

1.2、pom里边添加如下配置

XML 复制代码
<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、修改加载资源文件的方式

java 复制代码
// 加载资源文件方式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();
相关推荐
问道飞鱼5 小时前
【Springboot知识】开发属于自己的中间件健康监测HealthIndicate
spring boot·后端·中间件·healthindicate
黑风风7 小时前
深入理解Spring Boot Starter及如何自定义Starter
java·spring boot·后端
无名指的等待7128 小时前
SpringBoot实现一个Redis限流注解
spring boot·redis·后端
计算机-秋大田9 小时前
基于Spring Boot的小区疫情购物系统的设计与实现(LW+源码+讲解)
java·vue.js·spring boot·后端·课程设计
loveking69 小时前
SpringBoot调用华为云短信实现发短信功能
java·spring boot·华为云
小马爱打代码9 小时前
Spring Boot 事务管理:实战案例解析
spring boot
李长渊哦9 小时前
Spring Boot 约定大于配置:实现自定义配置
java·spring boot·后端
等什么君!10 小时前
学习springboot 的自动配置原理
java·spring boot·学习
微笑伴你而行10 小时前
spring boot 发送邮件验证码
java·数据库·spring boot
小咕聊编程10 小时前
【含文档+PPT+源码】基于SpringBoot和Vue的编程学习系统
vue.js·spring boot·学习