0

我在 Tomcat 中部署了我的 restlet.Component,在此,我读取了我的应用程序的配置文件:

this.properties.load(new FileInputStream("config/conf.properties));

我有一个java.io.FileNotFoundException

我的 webapp 结构是:

/mi-webapp
  /config
     conf.properties
  /WEB-INF
    /classes
    /lib
    web.xml

我怎样才能得到restlet的真实路径?我读过,ServletContext.getRealPath()但这是一个不好的做法,你怎么推荐?

4

1 回答 1

2

将 config 目录移到里面WEB-INF/classes并使用以下代码:

    URL url = this.class.getClassLoader().getResource("config/conf.properties");
    try {
        this.properties.load(url.openStream());
    } catch (IOException e) {
        System.out.println("Couldn't open/load properties file");
    }

这样,您就可以从属于您的应用程序范围内的类路径加载文件,并且不会导致任何安全漏洞。

于 2012-08-03T12:08:14.587 回答