在實(shí)際開發(fā)過(guò)程中,可能有時(shí)候,我們將文件放在resource下,在程序中需要獲取到文件路徑然后操作。比如,將公鑰放到resource文件夾下,在程序中需要獲取到這個(gè)公鑰的完整路徑。怎么操作?
?
需要訪問(wèn)的文件位置
獲取方法如下:
在yaml文件中配置公鑰文件名稱:
pem:
??public-key:?publickey.pem
代碼中獲取公鑰的絕對(duì)路徑:
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();
????????//?獲取文件的絕對(duì)路徑
????????String absolutePath?=?fileUrl.getPath();
????????//本地運(yùn)行的
????????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);
????????}?//非本地的。根據(jù)操作系統(tǒng)判斷.
????????
????????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;
????}
???
}
測(cè)試方法:
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);
????}
}
運(yùn)行后獲取到的公鑰路徑:文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-709283.html
運(yùn)行后結(jié)果文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-709283.html
到了這里,關(guān)于【已解決】Spring boot項(xiàng)目獲取到resource目錄下文件完整路徑怎么獲取 ?的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!