gpt4 book ai didi

java - 从内部 jar 中的类路径加载资源

转载 作者:行者123 更新时间:2023-12-01 16:42:24 24 4
gpt4 key购买 nike

我正在尝试将资源 csv 文件传递​​到我使用的 Sprint Boot 应用程序的 jar 内。尽管该方法获取的 Resource 参数 = ReactiveWebContext 资源 [src/main/resources/file.csv]

,但我总是得到文件不存在的响应
  • 在我的项目中,文件位于 src/main/resources/file.csv
  • 在 application.properties 中,我定义:

    file.csv.path=classpath*:file.csv
  • 在配置类中:

    @Configuration
    @PropertySources({
    @PropertySource(value = "classpath:application.properties", ignoreResourceNotFound =
    true)
    })
    public class BookConfiguration {
    }
  • 在内部 jar 中:配置类:

    @Configuration
    @Import({BoleroConfiguration.class})
    public class BoleroTestingInfraConfiguration {

    @Bean
    @ConditionalOnMissingBean
    public FileProvider fileProvider(@Value("${file.csv.path}") Resource fileResource) throws
    IOException {
    return new TestingProvider(fileResource);
    }
  • TestingProvider.class

    public class TestingProvider {

    private final Map<String, Set<Book>> idToBook;

    public TestingProvider(Resource fileResource) throws IOException {
    idToBook = getBooks(fileResource);
    }

    public Map<String, Set<Book>> getBooks(Resource fileResource) {
    if (fileResource == null || !fileResource.exists() ||
    !fileResource.getFile().exists() || !fileResource.getFile().isFile()) {
    return Optional.empty();
    }

    ....
    }
    }

感谢您的帮助

最佳答案

1) 您指定的路径不正确。它不应该是classpath*:file.csv,而是classpath:file.csv

2) 通常在 JAR 中不会有 src/main/resources/file.csv。文件src/main/resources/file.csv仅存在于源代码中。路径src/main/resources对应于JAR的级别。因此,文件 file.csv 将位于 JAR 的级别。

关于java - 从内部 jar 中的类路径加载资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61837683/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com