系列二十九、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();
相关推荐
白宇横流学长2 小时前
基于Spring Boot的校园考勤管理系统的设计与实现
java·spring boot·后端
_waylau2 小时前
“Java+AI全栈工程师”问答02:Spring Boot 自动配置原理
java·开发语言·spring boot·后端·spring
i220818 Faiz Ul2 小时前
高校教务|教务管理|基于springboot+vue的高校教务管理系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·论文·毕设·高校教务系统
贫民窟的勇敢爷们2 小时前
SpringBoot多环境配置全解+配置优先级管控
java·spring boot·后端
霸道流氓气质3 小时前
Spring @Scheduled 单线程陷阱:当设备重连阻塞了整个定时任务体系
java·spring boot·spring
欢璃4 小时前
表白墙案例
java·开发语言·jvm·spring boot·spring·maven·mybatis
代码漫谈4 小时前
Jetty vs Tomcat:Spring Boot应用场景最佳选择
spring boot·tomcat·jetty
贫民窟的勇敢爷们5 小时前
SpringBoot整合MyBatis-Plus极致实战,高效实现数据库CRUD与分页条件查询
数据库·spring boot·mybatis
阿丰资源5 小时前
基于SpringBoot的电影评论网站(含源码)
java·spring boot·后端
小码哥0685 小时前
2026版基于springboot的家政服务预约系统
java·spring boot·后端