docker部署jar包找不到资源文件-jar包报错找不到资源文件-代码示例

本文介绍了如何在一个Java类中实现同时支持IDEA调试和jar包运行时的资源文件访问,如FTL模板文件的处理,包括在本地调试和打包后的jar环境中正确加载配置文件的方法。
摘要由CSDN通过智能技术生成

同时支持idea调试和jar包运行

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class DocUtil {

    //此路径是其余方法进行调用,且只需要加载一次
    private static String sourceTemplatePath;
    // 读取文件名称
//    private static String[] ftlArray = {"shopPDF.ftl","yahei.ttf"};
    private static String[] ftlArray = {"krb5.conf", "user.keytab"};
    // resources下模板文件夹名称
//    private static String temPath = "templates/shopsummary/";
    private static String temPath = "";
    //模块名,针对多模块项目。单模块项目需删除
//    private static String modelPath = String.format("jeecg-module-system%sjeecg-system-biz", File.separator);

    static {
        //静态方法调用一次 
        sourceTemplatePath = createFtlFileByFtlArray();
    }

    // 获取临时文件路径
    public static String getSourceTemplatePath() {
        return sourceTemplatePath;
    }

    //获取临时文件模板路径
    public static String getRentalAgreementPath() {
        return sourceTemplatePath + ftlArray[0];
    }

    private static String createFtlFileByFtlArray() {
        String path = "";
        for (int i = 0; i < ftlArray.length; i++) {
            path = createFtlFile(temPath, ftlArray[i]);
            if (null == path) {
                log.info("not copy success:" + ftlArray[i]);
            }
        }
        return path;
    }

    private static String createFtlFile(String ftlPath, String ftlName) {
        try {
            //获取当前项目所在的绝对路径
            String proFilePath = System.getProperty("user.dir");
            log.info("project run path:" + proFilePath);
            //本地调试运行 
            ClassPathResource resource = new ClassPathResource("krb5.conf");
            if (resource != null && resource.getURI() != null && resource.getURI().getPath() != null) {
                String localFilePath = resource.getURI().getPath().replace("krb5.conf", "");
                log.info("localFilePath:" + localFilePath);
                return localFilePath;
            }

            //jar包运行
            String newFilePath = proFilePath + File.separator + "conf" + File.separator + ftlPath;
            newFilePath = newFilePath.replace("/", File.separator);
            log.info("newFilePath:" + newFilePath);

            File newFile = new File(newFilePath + ftlName);

            //当项目打成jar包会运行下面的代码,而且复制一份到conf路径下
            InputStream certStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(ftlPath + ftlName);
            byte[] certData = IOUtils.toByteArray(certStream);
            FileUtils.writeByteArrayToFile(newFile, certData);
            return newFilePath;
        } catch (IOException e) {
            log.error("复制文件失败--> 异常信息:" + e);
        }
        return "";
    }
}


使用:

String KRB5_FILE = DocUtil.getSourceTemplatePath() + "krb5.conf";

jar包运行时,会在jar同级目录下新建conf文件夹,并生成配置文件

相关参考:
docker部署jar包找不到资源文件-jar包报错找不到资源文件

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值