【发布时间】:2016-04-14 02:01:29
【问题描述】:
我在 Eclipse 中创建了动态 web 项目并将其添加到 ear 项目中。
图片资源放在src\resources\
代码使用资源:
@GET
@Path("bank")
@Produces("application/json")
public String getBank(@Context HttpHeaders headers) {
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("/resources/alfa.png").getFile());
String base64 = Utility.getBase64(file);
return "{\"icon\":\"" + base64 + "\"}";
}
在调试的时候就OK了。
当ear部署代码抛出异常时: java.io.FileNotFoundException: C:\apps\wildfly-8.2.0.Final\bin\content\TimerService.ear\timer.war\WEB-INF\classes\resources\alfa.png
所有资源都存在于ear-file中包含的war-file中。
怎么了?
【问题讨论】:
-
您不能将类路径资源引用为 java.o.File 对象,因为它们通常不会作为不同的文件系统对象存在文件系统。修改 Utility.getBase64 以采用 java.io.InputStream。
-
查看getResourceAsStream() vs FileInputStream 的所有答案以获取更多信息。我确定你的问题是骗人的,但我找不到合适的匹配
-
谢谢史蒂夫。 getResourceAsStream() 是解决方案。
标签: eclipse jakarta-ee jboss wildfly