解決springboot項目找不到resources目錄下的資源問題
springboot項目找不到resources目錄下的資源
問題描述:
將老的mvc項目轉(zhuǎn)為boot后找不到resources文件夾下的資源文件
原因:
war包采用的是tomcat部署,tomcat會對war包進行解壓,以及目錄的一些操作。而springboot使用jar包部署,服務(wù)器中是不存在相關(guān)目錄的。
環(huán)境:
springboot 2.2.2RELAESE
主要的API:
ClassPathResource classPathResource = new ClassPathResource(filePath); InputStream inputStream = classPathResource.getInputStream();
工具類
import java.io.File; import java.io.InputStream; import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; import org.springframework.core.io.ClassPathResource; public class FileUtil { public File getResourceFile(String filePath) throws Exception{ try { ClassPathResource classPathResource = new ClassPathResource(filePath); InputStream inputStream = classPathResource.getInputStream(); //生成目標文件 File somethingFile = File.createTempFile("DailyReportTemplate", ".xls"); try { FileUtils.copyInputStreamToFile(inputStream, somethingFile); } finally { IOUtils.closeQuietly(inputStream); } return somethingFile; } catch (Exception e) { throw new Exception(e); } } }
運行jar文件時,ClassPathResource無法讀取到資源文件的問題
問題場景:
在idea中運行,一切正常,資源文件都可以訪問到,但打成jar包后,使用java -jar的形式去啟動,就訪問不到resource下的資源文件了
網(wǎng)上搜了很多文章,但試了后都不好使
我的路徑是配置在properties文件中,然后讀取配置文件中的值,然后拼接文件路徑,再使用ClassPathResource去讀取的
開始時我配置文件中是這樣寫的:
#路徑中注意首尾不要有空格 service.config-root=static/service/ service.config-name=AppConfig.json
程序代碼讀出后拼接:
@Value("${service.config-root}") private String configRoot; @Value("${service.config-name}") private String configName; .....省略無關(guān)code..... public String getPath(){ String configPath = this.configRoot + this.configName; return configPath; }
但運行jar后,直接FileNotFoundException了
解決:
方案1:
主要是斜杠"\"和反斜杠"/"的問題,配置文件修改如下:
#路徑中注意首尾不要有空格 service.config-root=static\\tileservice\\ service.config-name=AppConfig.json
方案2:
使用"File.spearator"拼接路徑
service.config-root=static service.config-name=AppConfig.json @Value("${service.config-root}") private String configRoot; @Value("${service.config-name}") private String configName; .....省略無關(guān)code..... public static <T> T readJsonFromClassPath(Type type) throws IOException { //這里使用File.spearator拼接 ClassPathResource resource = new ClassPathResource(this.configRoot + File.spearator + this.configName); if (resource.exists()) { return JSON.parseObject(resource.getInputStream(), StandardCharsets.UTF_8, type, ..... } }
搞定!
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
- Springboot 項目讀取Resources目錄下的文件(推薦)
- 解決@springboottest注解無法加載src/main/resources目錄下文件
- springboot項目讀取resources目錄下的文件的9種方式
- springboot實現(xiàn)jar運行復(fù)制resources文件到指定的目錄(思路詳解)
- SpringBoot中讀取jar包中的resources目錄下的文件的三種方式
- SpringBoot如何讀取resources目錄下的文件
- SpringBoot實現(xiàn)本地上傳文件到resources目錄
- Springboot獲取jar包中resources資源目錄下的文件
- Springboot項目啟動不加載resources目錄下的文件問題
- SpringBoot下獲取resources目錄下文件的常用方法
相關(guān)文章
使用maven開發(fā)springboot項目時pom.xml常用配置(推薦)
這篇文章主要介紹了使用maven開發(fā)springboot項目時的pom.xml常用配置,本文給大家介紹的非常詳細,對大家的學(xué)習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-01-01Intellij Mybatis連接Mysql數(shù)據(jù)庫
最近在搞android的項目,在開發(fā)過程中遇到了好多問題,今天小編給大家說下mybatis連接MySQL數(shù)據(jù)庫的方法,感興趣的朋友跟著小編一起學(xué)習吧2016-10-10Spring Boot 與 Kotlin 使用JdbcTemplate連接MySQL數(shù)據(jù)庫的方法
本文介紹在Spring Boot基礎(chǔ)下配置數(shù)據(jù)源和通過 JdbcTemplate 編寫數(shù)據(jù)訪問的示例。感興趣的朋友跟隨腳本之家小編一起學(xué)習吧2018-01-01JavaWeb入門:ServletContext詳解和應(yīng)用
這篇文章主要介紹了Java ServletContext對象用法解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友可以參考下2021-07-07