Java 野蝇-从战争中获取资源

Java 野蝇-从战争中获取资源,java,jboss,getresource,wildfly-8,Java,Jboss,Getresource,Wildfly 8,我使用以下方法从WildFly中的WAR文件获取资源: this.getClass().getResource(relativePath) 当应用程序部署为“爆炸战争”时,它就会工作它过去也适用于压缩战争。昨天,我在Eclipse中对项目进行了一次清理和重建,但它只是停止了工作 当我检查资源根目录时: logger.info(this.getClass().getResource("/").toExternalForm()); 我明白了: file:/C:/JBoss/wildfly8.1.

我使用以下方法从WildFly中的WAR文件获取资源:

this.getClass().getResource(relativePath)
当应用程序部署为“爆炸战争”时,它就会工作它过去也适用于压缩战争。昨天,我在Eclipse中对项目进行了一次清理和重建,但它只是停止了工作

当我检查资源根目录时:

logger.info(this.getClass().getResource("/").toExternalForm());
我明白了:

file:/C:/JBoss/wildfly8.1.0.CR1/modules/system/layers/base/org/jboss/as/ejb3/main/timers/
所以,难怪它不起作用。这可能与JBoss模块加载有关,但我不知道这是错误还是正常行为

我在StackOverflow上发现了各种类似的问题,但没有适用的解决方案。建议之一是使用ServletContext,如下所示:

@Resource
private WebServiceContext wsContext;
...
ServletContext servletContext = (ServletContext)this.wsContext.getMessageContext()
        .get(MessageContext.SERVLET_CONTEXT);
servletContext.getResource(resourcePath);

但是,当我试图以这种方式获取MessageContext时,我得到了一个非法状态异常。所以我基本上被卡住了。有什么想法吗?

我终于放弃了,把我的资源文件放到了一个新的JBoss模块中,如本文所述


这是可行的,但缺点是有两个部署目标,因此事情更加复杂。从正面来看,WAR文件的大小减小了,如果只有部分资源发生了更改,我就不必重新部署应用程序。

我们也遇到了类似的问题,我们的错误是,我们试图通过原始路径访问静态资源,而不是使用资源提供的输入流。即使在部署非分解的.war文件时,以下代码也适用于我们

final URL resource = this.getClass().getResource(FILE);

try (final InputStream inputStream = resource.openStream();
     final InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
     final BufferedReader bufferedReader = new BufferedReader(inputStreamReader)) {
    // Use bufferedReader to read the content
} catch (IOException e) {
    // ...
}

我遇到了同样的问题,并没有将资源定义为共享模块,而是在WAR中使用ServletContextListener解决了这个问题

在contextInitialized方法中,我从ServletContextEvent获取ServletContext,并使用其getResource(“/WEB-INF/myResource”)获取WAR文件中资源的URL。似乎在ServletContextListener中,.getResource()方法按照预期解析为“/modules/system/layers/base/org/jboss/as/ejb3/main/timers/”url。然后,该URL可以存储在ServletContext中,供您的servlet或注入的应用程序处理的CDIBean使用

@WebListener
public class ServletInitializer implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        try {
            final ServletContext context = sce.getServletContext();
            final URL resourceUrl = context.getResource("/WEB-INF/myResource");
            context.setAttribute("myResourceURL", resourceUrl);
        } catch (final MalformedURLException e) {
            throw new AssertionError("Resource not available in WAR file", e);
        }
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {}
}


我最近试图弄清楚如何在我自己的Java war中访问文件。以下是java类和资源如何打包到war文件中:

WAR
 `-- WEB-INF
        `-- classes (where all the java classes are)
        `-- resourcefiles
                   `-- resourceFile1
我的目标文件是resourceFile1。要获取该文件,我只需在代码中执行以下操作:

InputStream inStream = this.class.getClassLoader().getResourceAsStream("resourcefiles/resourceFile1");

在这种情况下,资源文件需要与包含java类的classes文件夹位于同一文件夹中。希望其他人会觉得这很有帮助。

此示例代码适用于在openshift上部署和测试的wildfly。 我认为这是一个野蝇问题,我降落野蝇,并尝试在本地我也得到了错误


使用Spring和ServletContextResource时,Wildfly和not Explosed WAR遇到了同样的问题,我的解决方法如下:

[org.springframework.core.io.]Resource resource = new ServletContextResource(servletContext, "WEB-INF/classes/resource.png");
在同一@Service类中,我还有:

@Inject
private ServletContext servletContext;
我决定:

@Autowired
private final ApplicationContext ctx;
private final Path path = Paths.get("testfiles/load")

ctx.getRosource("classpath:" + path);

我阅读了这个解决方案,它引导我们在Wildfly中使用
getResourceAsStream(…)
而不是
getResource()
。我只是在Wildfly 19上使用从控制台部署的myApp.ear对其进行了测试。

从何处,您试图访问此代码?从JAX-RS web服务中的@GET方法。更具体地说,从无状态JAX-RS web服务中的@GET方法。谢谢。我现在没有时间检查,但也许它会帮助其他人。
@Inject
private ServletContext servletContext;
@Autowired
private final ApplicationContext ctx;
private final Path path = Paths.get("testfiles/load")

ctx.getRosource("classpath:" + path);