最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

解決springboot項(xiàng)目找不到resources目錄下的資源問(wèn)題

 更新時(shí)間:2021年08月11日 11:31:53   作者:the_fool_  
這篇文章主要介紹了解決springboot項(xiàng)目找不到resources目錄下的資源問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

springboot項(xiàng)目找不到resources目錄下的資源

問(wèn)題描述:

將老的mvc項(xiàng)目轉(zhuǎn)為boot后找不到resources文件夾下的資源文件

原因:

war包采用的是tomcat部署,tomcat會(huì)對(duì)war包進(jìn)行解壓,以及目錄的一些操作。而springboot使用jar包部署,服務(wù)器中是不存在相關(guān)目錄的。

環(huán)境:

springboot 2.2.2RELAESE

主要的API:

ClassPathResource classPathResource = new ClassPathResource(filePath);
InputStream inputStream = classPathResource.getInputStream();

工具類(lèi)

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();
            //生成目標(biāo)文件
            File somethingFile = File.createTempFile("DailyReportTemplate", ".xls");
            try {
                FileUtils.copyInputStreamToFile(inputStream, somethingFile);
            } finally {
                IOUtils.closeQuietly(inputStream);
            }
 
            return somethingFile;
        } catch (Exception e) {
            throw new Exception(e);
        }
    } 
}

運(yùn)行jar文件時(shí),ClassPathResource無(wú)法讀取到資源文件的問(wèn)題

問(wèn)題場(chǎng)景:

在idea中運(yùn)行,一切正常,資源文件都可以訪問(wèn)到,但打成jar包后,使用java -jar的形式去啟動(dòng),就訪問(wèn)不到resource下的資源文件了

網(wǎng)上搜了很多文章,但試了后都不好使

我的路徑是配置在properties文件中,然后讀取配置文件中的值,然后拼接文件路徑,再使用ClassPathResource去讀取的

開(kāi)始時(shí)我配置文件中是這樣寫(xiě)的:

#路徑中注意首尾不要有空格
service.config-root=static/service/
service.config-name=AppConfig.json

程序代碼讀出后拼接:

@Value("${service.config-root}")
private String configRoot; 
@Value("${service.config-name}")
private String configName;
 
.....省略無(wú)關(guān)code..... 
 
public String getPath(){
    String configPath = this.configRoot + this.configName;
    return configPath;
}

但運(yùn)行jar后,直接FileNotFoundException了

解決:

方案1:

主要是斜杠"\"和反斜杠"/"的問(wèn)題,配置文件修改如下:

#路徑中注意首尾不要有空格
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;
 
.....省略無(wú)關(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, .....
        }
    }

搞定!

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

稷山县| 平塘县| 渑池县| 务川| 大化| 乐东| 云霄县| 永顺县| 兰溪市| 珲春市| 东乌珠穆沁旗| 涡阳县| 怀远县| 张家口市| 长治县| 宽甸| 织金县| 温州市| 饶阳县| 汉源县| 鄂伦春自治旗| 从江县| 吉木萨尔县| 正安县| 定远县| 丰都县| 昌江| 溧阳市| 平远县| 班玛县| 铅山县| 望都县| 临邑县| 安达市| 临沧市| 磐石市| 新泰市| 修文县| 崇礼县| 辽源市| 漠河县|