相关疑难解决方法(0)

如何从spring资源获取文件

我有一个资源对象(org.springframework.core.io.ClassPathResource)。我需要获取 File 对象,但是resource.getFile() 抛出异常 File not Found。但调用后resource.getURI()我得到了结果

jar:file:/D: .... file.jar!/com//test/0be14958-3778-40bf-bd3e-ee605fcdd3f0/verify
Run Code Online (Sandbox Code Playgroud)

目录位于 jar 文件中。是否可以解决 ClassPathResource 的限制并创建 File 对象?

我尝试了一个新的 File(resource.getURI()) 但它失败并显示 java.lang.IllegalArgumentException: URI is not hierarchical

我有什么错?

java spring

2
推荐指数
1
解决办法
5160
查看次数

Maven 从依赖 jar src/main/resources 加载文件

我有一个 Spring Web 应用程序,它是作为 Maven 工件 A.war 创建的。它有一个带有工件 B.jar 的 maven 依赖项。工件 B 的 src/main/resources 有一个文件 test.txt。我想从工件 A.war 中读取文件 test.txt。当我分解 B.jar 时,我看到 test.txt 位于 jar 的根目录中。B.jar 最终出现在爆炸的 A.war 的 WEB-INF/lib 中。在 A 中,我尝试使用 File f = new ClassPathResource("test.txt").getFile(); 读取文件。

我收到 java.io.FileNotFoundException: 类路径资源 [test.txt] 无法解析为绝对文件路径,因为它不在文件系统中:jar:B.jar!/test.txt。我也试过 File f = new File(getClass().getResource("test.txt").toURI());

有人可以帮忙吗?

spring maven

2
推荐指数
1
解决办法
2403
查看次数

标签 统计

spring ×2

java ×1

maven ×1