如何在WEB-INF / class文件夹下获取资源路径(java ee web dynamic project) [英] How can I get path of resource under WEB-INF/class folder (java ee web dynamic project)

查看:364
本文介绍了如何在WEB-INF / class文件夹下获取资源路径(java ee web dynamic project)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Eclipse中开发Web动态项目。一个名为'users.txt'的文件位于classes文件夹(WEB-INF / classes / users.txt)下。

I develop web dynamic project in Eclipse. One file, file named 'users.txt' is located under classes folder (WEB-INF/classes/users.txt).

如何获取此相对路径类中的文件(基类,而不是servlet类)?我将使用该路径追加几行文字。

How I can get relative path of this file in class (base class, not servlet class)? I will use that path for append several lines of text.

Christian这是用于阅读的代码。问题是我不知道如何在具有相对路径的同一文件中创建用于写入(输出)的对象。

Christian this is the code that works for reading. Problem is I don't know how to create object for writing (output) in the same file with relative path.

public Products(){
    try{
        InputStream in = getClass().getClassLoader().getResourceAsStream("products.txt");    
        BufferedReader br = new BufferedReader(new InputStreamReader(in));

        readProducts(br);
        in.close();
            br.close();


    }catch(Exception e){
        System.out.println("File doesn't exists");
    }
    }

    public void readProducts(BufferedReader in) throws NumberFormatException, IOException{

        String id="";
        String name="";
        double price=0;

        StringTokenizer st;
        String line;

        while((line=in.readLine())!=null){
            st=new StringTokenizer(line,";");
            while(st.hasMoreTokens()){
                id=st.nextToken();
                name=st.nextToken();
                price=Double.parseDouble(st.nextToken());           
            }           
            products.put(id,new Product(id,name,price));
        }
    }


推荐答案

一般来说说到你不应该依赖修改你的web应用程序目录中的文件。

Generally speaking you should not rely on modifying files in your web application directory.

其中一个原因是servlet容器没有义务提取你的。 war 文件到fs,理论上它可以从内存运行你的web应用程序。是的,Tomcat和大多数容器都解压 .war 但Java EE规范中没有任何内容表明他们必须这样做。

One of the reasons is that servlet container is not obligated to extract your .war file to the fs, it could, in theory, run your web application from memory. Yeah, Tomcat and most of the containers do unpack .war but there is nothing in Java EE specs that says that they must do this.

无论如何,如果您认为值得冒这个风险,可以使用 ServletContext.getRealPath 获取webapp目录中文件的位置。

Anyway, if you think that this worth the risk you can use ServletContext.getRealPath to get location of the file inside your webapp directory.

这应该看起来类似这样的事情:

This should look something like this:


  1. 在你的webapp根目录下创建一个文件 myfile (应该是在 .war 中的位于 WEB-INF 位于同一文件夹中。

  2. 在servlet中执行类似的操作(或者可以访问servlet上下文的任何地方):

  1. Create a file myfile under your webapp root (should be located in the same folder as WEB-INF in your .war).
  2. do something like this in your servlet (or anywhere where you can access servlet context):

String filePath = getServletContext( ).getRealPath(myfile);

String filePath = getServletContext().getRealPath("myfile");

请注意,要使其正常工作,您应该能够获取带请求的文件,如: http://< host>:< port> /< contextPath> / myfile (详见方法文档)

Note that in order for this to work you should be able to fetch the file with request, like: http://<host>:<port>/<contextPath>/myfile (see method docs for details)

这篇关于如何在WEB-INF / class文件夹下获取资源路径(java ee web dynamic project)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆