读取JSF应用程序中的资源文件 [英] Read a resource file inside JSF application

查看:131
本文介绍了读取JSF应用程序中的资源文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在我的JSF应用程序中获取一个资源文件.

I need to grab a resource file inside my JSF application.

InputStream input = new FileInputStream("filename.xml");

但是,系统找不到与sample.xhtml网页位于同一文件夹中的filename.xml文件.如何获得它的InputStream?

However, the system does not find filename.xml file, which is in the same folder as the sample.xhtml web page. How do I get an InputStream of it?

推荐答案

FileInputStream直接在本地磁盘文件系统上工作.它对应用程序正在运行的上下文一无所知.它对WAR文件结构,类路径资源和Web资源一无所知.

The FileInputStream works directly on the local disk file system. It knows nothing about the context the application is running on. It absolutely doesn't know anything about WAR file structures, classpath resources and web resources.

您传递给FileInputStream的任何相对路径(即,任何不以驱动器号或斜杠开头的路径)都相对于当前工作目录进行解释,该目录是在Java Virtual刚打开时打开的文件夹通过java.exe命令启动计算机.您可以按照以下步骤确定确切路径:

Any relative path (i.e. any path not starting with a drive letter or a slash) which you pass to FileInputStream is interpreted relative to the current working directory, which is the folder which is opened at exactly the moment the Java Virtual Machine is started by java.exe command. You can figure the exact path as follows:

System.out.println(new File(".").getAbsolutePath());

很有可能这不是您想要的位置不是.否则,您将不会首先问这个问题.通常,它会根据启动服务器的方式引用IDE的工作区文件夹,服务器的二进制文件夹或命令提示符中当前打开的文件夹.

Chances are very big that this is not the location you expected to be. Otherwise you wouldn't have asked this question in first place. Usually it refers the IDE's workspace folder, or the server's binary folder, or the currently opened folder in command prompt, depending on the way how you started the server.

如果文件作为类路径资源放置在类路径中,即您将其放置在com.example包的"Java Sources"中,则应按以下方式获取文件的InputStream:

If the file is placed in the classpath as a classpath resource, i.e. you placed it in "Java Sources" in a com.example package, then you should be obtaining an InputStream of it as follows:

InputStream input = Thread.currentThread().getContextClassLoader().getResourceAsStream("com/example/filename.xml");
// ...

或者,如果保证与当前类相同的类加载器可见,并且您不在静态上下文中,则也可以这样做(是的,与上下文类加载器相比,前导斜杠!):

Or, if it is guaranteed to be visible to the same classloader as the current class, and you're not in static context, then you could also do so (yes, with a leading slash as opposed to the context class loader!):

InputStream input = getClass().getResourceAsStream("/com/example/filename.xml");
// ...

或者,如果它与当前类位于同一程序包中:

Or, if it is in the same package as the current class:

InputStream input = getClass().getResourceAsStream("filename.xml");
// ...

或者,如果该文件作为Web资源放置在Web内容中,即您将其放置在"Web Content"中的同一文件夹中,也可以找到/WEB-INF/META-INF等,则您应该获得它的InputStream,如下所示:

Or, if the file is placed in the webcontent as a web resource, i.e. you placed it in "Web Content" in the same folder as where you can also find /WEB-INF, /META-INF and so on, then you should be obtaining an InputStream of it as follows:

InputStream input = FacesContext.getCurrentInstance().getExternalContext().getResourceAsStream("/filename.xml");
// ...

另请参见:

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