SpringBoot Docker部署读取Resource下的文件

SpringBoot Docker部署读取Resource下的文件

最近在使用Docker部署SpringBoot项目时,同事编写的读取Resource下的文件方法读取不到文件信息,就把这个问题解决方案记录一下。

这里待读取文件路径为 resource/db/Clickhouse.sql
文件路径
以下三种方法在开发环境(IDE中)和生产环境(linux部署成jar包)都可以读取到
第一种

ClassPathResource classPathResource = new ClassPathResource("db/Clickhouse.sql");
InputStream inputStream =classPathResource.getInputStream();

第二种

InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("db/Clickhouse.sql");

第三种

InputStream inputStream = this.getClass().getResourceAsStream("/db/Clickhouse.sql");

同事的代码本地可以读取文件信息,Docker部署读取不到文件信息,主要原因是springboot内置tomcat,打包后是一个jar包,因此通过文件读取获取流的方式行不通,因为无法直接读取压缩包中的文件,读取只能通过流的方式读取。

	/**
     * 获取项目中配置文件内容
     * @param path 文件相对路径
     * @return java.lang.String
     */
    public static String readPropertiesFile(String path){
        URL resource = Util.class.getClassLoader().getResource(path);
        log.info("resource===>"+resource+"path===>"+path);
        if (resource==null){
           throw new BizException("获取资源失败");
        }
        String filePath =resource.getFile();
        File file = new File(filePath);
        StringBuilder builder=new StringBuilder();
        try {
            log.info("filePath===>"+filePath+"====file==="+file);
            FileInputStream fis = new FileInputStream(file);
            log.info("fis===>"+fis);
            InputStreamReader inputStreamReader = new InputStreamReader(fis, StandardCharsets.UTF_8);
            BufferedReader br = new BufferedReader(inputStreamReader);
            String data;
            while ((data=br.readLine())!=null){
                builder.append(data);
            }
            br.close();
            inputStreamReader.close();
            fis.close();
        } catch (IOException e) {
            throw new BizException("获取sql文件失败");
        }
        return builder.toString();
    }

我的代码用的是上面说的第一种方法:第一种方案本人亲测可以实现(另外两种未进行Docker部署测试),直接查看底层代码都是通过类加载器读取文件流,类加载器可以读取jar包中的编译后的class文件,当然也是可以读取jar包中的文件流了

	/**
     * 已流的方式来获取项目中配置文件内容(wins与linux均兼容)
     * @param path 文件相对路径
     * @return java.lang.String
     */
    public static String readPropertiesFile(String path){
        //获取资源
        ClassPathResource resource = new ClassPathResource(path);
        if (resource==null){
           throw new BizException("获取资源失败");
        }
        StringBuilder builder=new StringBuilder();
        try {
            //获取文件流
            InputStream fis = resource.getInputStream();
            //设置编码格式
            InputStreamReader inputStreamReader = new InputStreamReader(fis, StandardCharsets.UTF_8);
            BufferedReader br = new BufferedReader(inputStreamReader);
            String data;
            while ((data=br.readLine())!=null){
                builder.append(data);
            }
            br.close();
            inputStreamReader.close();
            fis.close();
        } catch (IOException e) {
            throw new BizException("获取sql文件失败");
        }
        return builder.toString();
    }

最后查询资料还发现了用hutool工具包的ResourceUtil可以实现

import cn.hutool.core.io.resource.ResourceUtil;
//用hutool工具包的ResourceUtil
public static InputStream getTemplateXslmAsStream() throws IOException {
        return ResourceUtil.getStream("templates/gojob/excel/job_define.xlsm");
    }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值