2017-07-10 49 views
0

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

<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文件中找到该文件。

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

我也试过在Eclipse执行实施时无需访问(我知道,在执行战争文件时,会导致错误)以下:

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

UPDATE:

现在我可以在Eclipse中执行成功的实现。我先用注解@Resource试过,但改为@context

@Context 
ServletContext servletContext; 

然后,我添加此行:

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

感谢

回答

2

我不知道如果这能帮助你如此,但你可以使用HttpServletRequest request从您的doGet()(的doPost(),或他人的一个),尝试:

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

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

希望这有助于,

注:HttpServletRequest的它自身具有相同名称的方法,而是一个已被弃用,这样你就会知道它的错误之一。

在评论中讨论后:

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

找到文件:tomcat的/ conf目录/,实际上是运行Tomcat实例的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问题:

我用的球衣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等功能位于

几年前在我使用了Servlet过滤器SOAP服务和来自的doFilter()函数检索请求对象类中,

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

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

在这两种情况下,你有ServletRequest对象或ServletContext对象,所以你可以它放到该文件读取位置解决方案

亲切的问候

+0

由于我的maven规范,/ WebContent /的内容最终在.war文件的根目录中。我只是想能够在Eclipse中执行实现(启动Tomcat,将Project添加为Eclipse中的服务器资源)以及.war文件。 – Matzka

+0

也许我误解了,你的意思是你需要从eclipse中的位置读取文件吗? 这可能会像“/Users/user/workspace/project/WebContent/text.txt” 比你可以在你的开发机器上使用系统属性来覆盖默认的生产环境... – mzijdemans

+0

我正在开发服务在Eclipse中。战争文件将被集成到另一个Tomcat实例中。我需要一种读取txt文件的方式,该文件在Eclipse中执行服务时用于测试pruposes以及在生产系统中执行.war文件时工作。在Eclipse中执行程序时,不会触及.war文件。重写读取操作可能听起来像是个好主意。 – Matzka