【发布时间】:2020-09-02 08:49:51
【问题描述】:
我正在尝试将资源 csv 文件传递到使用我的 Sprint Boot 应用程序的 jar 内部。 我总是得到一个文件不存在的响应,尽管方法 get = ReactiveWebContext resource [src/main/resources/file.csv] 的 Resource 参数
- 在我的项目中,我的文件位于 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 中:Config 类:
@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(); } .... } }
感谢您的帮助
【问题讨论】:
标签: java spring-boot file-io jar resources