maven - 在 WAR 中读取文件

标签 maven tomcat war

我正在使用 Maven 和 Tomcat 开发一个小型 RESTFul 项目。在我的实现中,我必须读取一个 txt 文件。 目前我正在测试 Eclipse 中的实现。使用绝对路径时,我可以毫无问题地执行我的代码。由于要生成war文件,所以需要将txt文件封装在里面。使用 Maven 和

    <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.3</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.6</version>
            <configuration>
                <warSourceDirectory>WebContent</warSourceDirectory>
                <failOnMissingWebXml>false</failOnMissingWebXml>
            </configuration>
        </plugin>
    </plugins>
</build>

我的war文件包含存放在最高层的txt文件。您建议从 war 文件中的 txt 文件中读取什么?我在这里找到了几个线程,但所有尝试都失败了。我需要一个规范,让我可以在 Eclipse 中测试时以及在 Tomcat 中直接执行文件时读取文件。

当在 Eclipse 中执行实现时,此代码不起作用,因为未找到文本文件。该文件位于 WebContent 文件夹中,生成快照/war 后,您可以在 war 文件中找到该文件。

            InputStreamFactory isf = new InputStreamFactory() {
            public InputStream createInputStream() throws IOException {
                return new FileInputStream("/text_de.txt");
            }
        };

在没有访问权限的情况下在 Eclipse 中执行实现时,我还尝试了以下操作(我知道,这会在执行 war 文件时导致错误):

return new FileInputStream("/WebContent/text_de.txt");

更新:

现在我可以成功地在 Eclipse 中执行实现。我先用注解@Resource试了一下,后来改成@Context

    @Context
ServletContext servletContext;

然后,我添加了这一行:

String fileLocation = System.getProperty("com.example.resourceLocation",servletContext.getRealPath("/text_de.txt"));

谢谢

最佳答案

我不确定这对您的情况是否有帮助,但是您可以使用来自您的 doGet()(doPost() 或其他之一)的 HttpServletRequest 请求,尝试:

return new FileInputStream(request.getServletContext().getRealPath("/text_de.txt"));

如果您使用解压程序解压缩 .war 文件,您可以确定所需的路径,如果“/text_de.txt”可能不正确,但很多时候/WebContent/的内容最终位于根目录中.war 文件...

希望对你有帮助,

注意:HttpServletRequest 本身有一个同名的方法,但那个方法已被弃用,所以你会知道它是错误的。

在评论中讨论后:

在开发过程中你需要一个不同的位置,

找到实际运行的tomcat实例的文件:tomcat/conf/catalina.properties。我不确定 Eclipse 是否正在运行它,或者您将 Eclipse 指向 tomcat-home 文件夹

添加一行 com.example.resourceLocation=/full/path/to/eclipse/project/text_de.txt

比使用类似的东西:

String fileLocation = System.getProperty(
    "com.example.resourceLocation",
    request.getServletContext().getRealPath("/text_de.txt")); 
    //this second parameter is a default value you can provide yourself
    //if the property does not exists it chooses the second as return value    

针对非 servlet 问题的回应:

我正在使用 Jersey link , 在这里我使用这样的东西

@Context
private HttpServletRequest httpRequest;

@GET
@Path("file")
@Produces(MediaType.APPLICATION_JSON)
public Response getFile () {
    String fileLocation = System.getProperty(
        "com.example.resourceLocation",
        this.httpRequest.getServletContext().getRealPath("/text_de.txt")); 
        //this second parameter is a default value you can provide yourself
        //if the property does not exists it chooses the second as return value

    // do something with fileLocation
    FileInputStream fi = new FileInputStream(fileLocation);
    return Response.status(Response.Status.OK)
                .entity("body")
                .build();
}

在响应@POST 或@GET 等函数所在的类中

几年前在 SOAP 服务中我使用了一个 servlet 过滤器并从 doFilter() 函数中检索请求对象,

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        this.context = request.getServletContext( );

Eclipse 可以使用项目的上下文菜单创建过滤器...

在这两种情况下,您都有 servletRequest 对象或 servletContext 对象,因此您可以将其放入文件读取器位置解决方案

亲切的问候

关于maven - 在 WAR 中读取文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45011946/

相关文章:

java - 如何对在 Apache Tomcat 上运行的 war 应用程序使用版本控制

java - 如何在与 Maven 的 war 中更新 MANIFEST

maven - 编码器类 (org.owasp.esapi.reference.DefaultEncoder) CTOR 抛出异常

java.lang.ClassNotFoundException : org. json.JSONObject

tomcat - 找不到根目录中的 Grails 文件

java - Tomcat 无法启动 Grails 应用程序

weblogic - 通过weblogic部署war文件

java - eclipse插件类路径问题

eclipse - Service Loader 配置文件未正确展开

java - HTTP 状态 404。描述请求的资源不可用