【已解决】Spring boot项目获取到resource目录下文件完整路径怎么获取 ?

在实际开发过程中,可能有时候,我们将文件放在resource下,在程序中需要获取到文件路径然后操作。比如,将公钥放到resource文件夹下,在程序中需要获取到这个公钥的完整路径。怎么操作?

 

需要访问的文件位置

获取方法如下:

在yaml文件中配置公钥文件名称:

pem:
  public-key: publickey.pem

代码中获取公钥的绝对路径:

package com.kaigejava.config.pem;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ClassPathResource;

import java.io.IOException;
import java.net.URL;


/**
 * @author kaigejava
 * @since 2023/6/27 9:48
 */
@Slf4j
@Data
public class PemBean {

    /**
     * 公钥名称
     */
    private static String pubKey = "publickey.pem";
    /**
     * 公钥地址
     */
    private static String pubKeyFilePath = "files/pem/";
    /**
     * zip文件地址
     */
    private static String zipDataBasePath = "files/zip/";

  

    public static String getPublicKey() {
        try {
            return function1(pubKey);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return pubKey;
    }


    private static String getPrePath(String fileName, String path) throws IOException {
        ClassPathResource classPathResource = new ClassPathResource(fileName);
        URL fileUrl = classPathResource.getURL();
        // 获取文件的绝对路径
        String absolutePath = fileUrl.getPath();
        //本地运行的
        int index = -1;
//注意:这里修改成你自己的路径
String s = "kaigejava/bin";
        String os = System.getProperty("os.name");
        if (absolutePath.contains("target")) {
            index = absolutePath.indexOf("target");
            absolutePath = absolutePath.substring(1, index);
        } //非本地的。根据操作系统判断.
        
        if (os != null && os.toLowerCase().startsWith("windows")) {
            log.info("windows");
            if (absolutePath.startsWith("file:")) {
                if (absolutePath.contains(s)) {
                    index = absolutePath.indexOf(s);
                }
                absolutePath = absolutePath.substring("file:".length() + 1, index + s.length());
            }
        } else if (os != null && os.toLowerCase().startsWith("linux") || isMacOSX(os)) {
            log.info("linux");
//注意:这里修改成你自己的路径
            if (absolutePath.contains("kaigejava-docs")) {
                s = "usb-drive";
            }
            if (absolutePath.startsWith("file:")) {
                if (absolutePath.contains(s)) {
                    index = absolutePath.indexOf(s);
                }
                absolutePath = absolutePath.substring("file:".length(), index + s.length());
            }
        }
        absolutePath = absolutePath + path;
        return absolutePath;
    }


    public static boolean isMacOSX(String os) {
        return os.indexOf("mac") >= 0 && os.indexOf("os") > 0 && os.indexOf("x") > 0;
    }

    public static String function1(String fileName) throws IOException {
        String prePath = getPrePath(fileName, pubKeyFilePath);
        prePath = prePath + fileName;
        return prePath;
    }

    public static String getZipDataBasePath() {
        try {
            return getPrePath(pubKey, zipDataBasePath);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return zipDataBasePath;
    }


   

}

测试方法:

package com.kaigejava;

import com.kaigejava.config.pem.PemBean;
import xxx.sm2.SM2;
import org.bouncycastle.math.ec.ECPoint;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.UnsupportedEncodingException;
import java.util.Base64;

@SpringBootTest
class UsbDriveApplicationTests {


    @Test
    public void getPKey() throws UnsupportedEncodingException {
        SM2 sm = new SM2();
        String path = "";
        path = PemBean.getPublicKey();
        System.out.println("公钥地址:" + path);
        ECPoint publicKey = sm.importPublicKey(path);
        byte[] data = sm.encrypt("haha", publicKey, "UTF-8");
        Base64.Encoder encoder = Base64.getEncoder();
        System.out.println("加密后:" + encoder.encodeToString(data));
    }

    @Test
    public void getZipPath() throws UnsupportedEncodingException {
        String path = PemBean.getZipDataBasePath();
        System.out.println("ZIP地址:" + path);
    }
}

运行后获取到的公钥路径:

运行后结果

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

凯哥Java

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值