系列二十九、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 小时前
SpringBoot 项目集成 GeoServer 源码,实现地图服务发布
java·spring boot·后端
leoZ2313 小时前
2026-09-09-springboot-cloud-deploy-pitfalls
java·前端·javascript·vue.js·人工智能·spring boot·后端
孫治AllenSun6 小时前
【LangChain4J-09】开发学习知识点
spring boot·后端·学习
林石工作室7 小时前
体育直播APP从0到1搭建指南:技术选型、核心模块与部署实战
spring boot·流媒体·app搭建·体育直播app·星逐赛事
LXMXHJ8 小时前
springboot中的线程操作
java·spring boot·后端·线程
行百里er10 小时前
BlockingQueue:异步批量上报
spring boot·spring cloud·监控
武哥聊编程12 小时前
【AI实战项目】AI Agent数据分析平台,基于SpringAI+Springboot+Vue+Agent的电商数据分析平台
人工智能·spring boot·数据分析·springai
计算机毕设定制辅导-无忧学长12 小时前
《基于SpringBoot的庭院玫瑰栽培养护知识交互式科普平台设计与实现》
java·spring boot·后端·毕业设计·个性化推荐·协同过滤算法·庭院玫瑰栽培养护知识科普平台
骇客野人12 小时前
SpringBoot数十Jar包批量生产部署落地实施方案(Shell+Systemd完整版)
spring boot·后端·jar
Json____13 小时前
从零构建家政服务平台:一套全栈架构如何打通管理端与移动端-java-springboot
java·spring boot·后端·架构·毕设·wwwoop.com