简体   繁体   English

如何从耳朵/战争/罐子中读取文件?

[英]How to read file from ear/war/jar?

I have EAR which contains another WAR. 我的EAR包含另一个WAR。 The project structure is shown below: 项目结构如下所示:

project.ear
 |-- lib
 |-- META-INF
 |-- project-web.war
 |    |-- META-INF
 |    |-- WEB-INF
 |    |    |-- classes
 |    |    |    `-- com
 |    |    |         `-- example
 |    |    |              `-- services
 |    |    |                   `-- ListPageService.class
 |    |    |-- lib
 |    |    |-- web.xml
 |    |    `-- weblogic.xml
 |    `-- content.html
 `-- project-services.jar

In the WAR, there's a JAX-RS class ListPageService which needs to read the content.html file. 在WAR中,有一个JAX-RS类ListPageService ,它需要读取content.html文件。 How do I access that file? 如何访问该文件?

Edit: this answer is no longer applicable to the question as it is formulated now. 编辑:此答案不再适用于现在提出的问题。 The suggested method works only to get resources from the classpath. 建议的方法仅可从类路径中获取资源。

Have a look at the Class.getResourceAsStream method. 看看Class.getResourceAsStream方法。 It allows you to access files found in the class path. 它允许您访问在类路径中找到的文件。

The usual way to get at resources in a WAR file is via ServletContext.getResource or getResourceAsStream . 获取WAR文件中资源的通常方法是通过ServletContext.getResourcegetResourceAsStream You should be able to get access to the ServletContext in a JAX-RS class by declaring a field annotated with javax.ws.rs.core.Context 通过声明一个用javax.ws.rs.core.Context注释的字段,您应该能够访问JAX-RS类中的ServletContext

@Context ServletContext servletContext;

then in the request handling method you can say 然后在请求处理方法中,您可以说

URL content = servletContext.getResource("/content.html");
// alternatively
// InputStream content = servletContext.getResourceAsStream("/content.html");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM