Java获取ClassPath下的预存文件

一 背景

        在以前的单体应用中我们可以将文件存放在服务器中的指定目录,然后通过代码直接获取,而当我们在使用Docker容器发布的时候,由于每次镜像打包都会将文件清除(本人目前的理解),而挂载目录使用不是很熟练(我的理解挂载目录可以解决),所以通过下列方式来实现ClassPath下预存文件处理。

二 示例分析

        直接向ClassPath目录下的文件读成文件流

import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
try {
       Resource resource = new ClassPathResource("/cert/**/wxjsapiapiclient_cert.p12");
       File sourceFile = resource.getFile();
       InputStream certStream = new FileInputStream(sourceFile);
       this.certData = new byte[(int) sourceFile.length()];
       certStream.read(this.certData);
       certStream.close();
     } catch (Exception e) {
       System.err.print(e);
     }

        获取当前项目所在的绝对路径。

String path = System.getProperty("user.dir");

        获取当前项目中,某相对路径下资源的绝对路径。

/**
 * @author calm_encode
 */
public class ClassPathUtil {

    public String getClassPath(String relativePath){

        //获取ClassPath, relativePath 为资源的相对路径
        String path = ClassLoader.getSystemResource(relativePath).getPath();
        return path;
    }
}

三 完整示例

String path = System.getProperty("user.dir");
System.out.println("path = " + path);

String paths = ClassLoader.getSystemResource("common-images/photoBook/back_cover.png").getPath();
System.out.println(paths);

        结果:

         个人使用的一些方式,希望对各位有用。后续完全理解挂载目录和Docker镜像容器细节再来完善。待续。。。

Java 中,从 classpath文件获取资源可以使用以下几种常见方法: ### 使用 `ClassLoader` 的 `getResource` 和 `getResourceAsStream` 方法 `ClassLoader` 提供了从 classpath 加载资源的功能。`getResource` 方法返回一个 `URL` 对象,而 `getResourceAsStream` 方法返回一个 `InputStream` 对象,用于读取资源内容。 ```java import java.io.InputStream; import java.net.URL; public class ClasspathResourceExample { public static void main(String[] args) { // 获取 ClassLoader ClassLoader classLoader = ClasspathResourceExample.class.getClassLoader(); // 使用 getResource 获取资源的 URL URL resourceUrl = classLoader.getResource("yourFolder/yourFile.txt"); if (resourceUrl != null) { System.out.println("Resource URL: " + resourceUrl); } // 使用 getResourceAsStream 获取资源的输入流 InputStream inputStream = classLoader.getResourceAsStream("yourFolder/yourFile.txt"); if (inputStream != null) { System.out.println("Resource input stream is available."); } } } ``` ### 使用 `Class` 的 `getResource` 和 `getResourceAsStream` 方法 `Class` 类也提供了 `getResource` 和 `getResourceAsStream` 方法。与 `ClassLoader` 不同的是,`Class` 的 `getResource` 方法会根据类的包路径来解析资源路径。如果资源路径以 `/` 开头,则会从 classpath 根目录开始查找。 ```java import java.io.InputStream; import java.net.URL; public class ClassResourceExample { public static void main(String[] args) { // 使用 getResource 获取资源的 URL URL resourceUrl = ClassResourceExample.class.getResource("/yourFolder/yourFile.txt"); if (resourceUrl != null) { System.out.println("Resource URL: " + resourceUrl); } // 使用 getResourceAsStream 获取资源的输入流 InputStream inputStream = ClassResourceExample.class.getResourceAsStream("/yourFolder/yourFile.txt"); if (inputStream != null) { System.out.println("Resource input stream is available."); } } } ``` ### 使用 `java.util.ResourceBundle` 加载资源 如果需要加载属性文件等资源,可以使用 `java.util.ResourceBundle` 类。 ```java import java.util.ResourceBundle; public class ResourceBundleExample { public static void main(String[] args) { // 加载资源束 ResourceBundle resourceBundle = ResourceBundle.getBundle("yourFolder.yourPropertiesFile"); // 获取属性值 String value = resourceBundle.getString("yourKey"); System.out.println("Property value: " + value); } } ``` ### 使用 `java.nio.file.Paths` 和 `ClassLoader` 结合 可以通过 `ClassLoader` 获取资源的 URL,然后使用 `java.nio.file.Paths` 将其转换为 `Path` 对象,以便进行文件操作。 ```java import java.io.IOException; import java.net.URL; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; public class PathResourceExample { public static void main(String[] args) throws IOException { ClassLoader classLoader = PathResourceExample.class.getClassLoader(); URL resourceUrl = classLoader.getResource("yourFolder/yourFile.txt"); if (resourceUrl != null) { Path path = Paths.get(resourceUrl.getPath()); byte[] fileContent = Files.readAllBytes(path); System.out.println("File content length: " + fileContent.length); } } } ```
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值