【问题标题】:Resource file not found after war file deployed upon wildfly8 [duplicate]在wildfly 8上部署war文件后找不到资源文件[重复]
【发布时间】: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


【解决方案1】:

这样它应该可以工作

@GET
@Path("bank")
@Produces("application/json")
public String getBank(@Context final HttpServletRequest request) {
  ClassLoader classLoader = request.getServletContext().getClassLoader();
  File file = new File(classLoader.getResource("/resources/alfa.png").getFile());
  String base64 = Utility.getBase64(file);
  return "{\"icon\":\"" + base64 + "\"}";
}

【讨论】:

    猜你喜欢
    • 2019-10-27
    • 1970-01-01
    • 1970-01-01
    • 2020-05-15
    • 2015-11-21
    • 1970-01-01
    • 2014-07-11
    • 2010-09-13
    • 2016-06-13
    相关资源
    最近更新 更多