在实际开发过程中,可能有时候,我们将文件放在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);
}
}
运行后获取到的公钥路径:
运行后结果