一、Spring Boot打成jar包后,resources目錄下的資源文件獲取不到
1.1、問(wèn)題描述
? ? ? ? 在做公司業(yè)務(wù)開(kāi)發(fā)時(shí),有一個(gè)地方是使用EasyExcel模板技術(shù)進(jìn)行文件上傳,測(cè)試環(huán)境是OK的,但是和前后端聯(lián)調(diào)驗(yàn)證測(cè)試通過(guò)后,上傳到生產(chǎn)環(huán)境就不行了,后來(lái)也是花費(fèi)了很多時(shí)間才找出其中的問(wèn)題,以下為解決方案。文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-787971.html
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();
?文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-787971.html
到了這里,關(guān)于系列二十九、Spring Boot打成jar包后,resources目錄下的資源文件獲取不到的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!