首页 > 解决方案 > Spring Boot Jar 需要从类路径中读取 Keystore 作为文件

问题描述

src/main/resources我有一个文件夹files。目标是读取文件并在下面的代码中使用它们

@PostConstruct
public void init() {
  ....
  File jks = null;
  try {
      final FileInputStream fin = new FileInputStream(ResourceUtils.getFile("classpath*:files/prod-apicrypt.cer"));
    jks = ResourceUtils.getFile("classpath*:files/mykeystore.jks");
    .....
}catch(Exception e){
//handling
}
...

这在 IDE 中有效,但当mvn spring-boot:run我创建 jar 并尝试运行它时,我FileNotFoundException从 .cer 文件中获取。我想密钥库也会发生同样的情况。

问题是我不能像在其他回复中发现的那样,只做一个 Inputstream 并逐行读取文件。

我认为我必须缺少一些基本的东西,因为 Spring Boot 肯定有一种从类路径读取文件的方法。我只是不知道是什么。

标签: javafilespring-bootclasspath

解决方案


推荐阅读