spring :cannot be resolved to absolute file path because it does not reside in the file system: jar

class path resource [test-es-index.txt] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/opt/app.jar!/BOOT-INF/classes!/test-es-index.txt

spring boot 打包后 jar文件,执行jar 后,内部程序访问 jar 中的 test-es-index.txt 报错。
打印各种日志发现,都存在,使用的是 resource.getFile(),一直报 上面错误。

换成文件流读取就没有问题了

ClassPathResource resource = new ClassPathResource("test-es-index.txt");
            if(!resource.exists()){
                log.info("test-es-index.txt 文件不存在");
            }else{
            String mapping = IOUtils.toString(inputStream,"utf-8");;
}

IOUtils 使用的是 apache 工具包

import org.apache.commons.io.IOUtils;

你可能感兴趣的:(java)