使用GlassFish的WebApplication中的文件路径 [英] file path in WebApplication using GlassFish
问题描述
我正在使用一个类来获取源文件夹下的属性文件。但它不起作用!检查后,我发现使用
File f = new File(/ src / ss.properties)的默认路径;
不是Web应用程序路径,而是glassfish配置路径!如果我想获取存储在classes路径中的属性文件,我该怎么办?通常情况下,默认路径是项目路径,您知道。
我已经使用了 ClassLoader.getResourceAsStream(sss)
。但它返回null!我确定文件名是正确的,因为我已经在另一个简单的Java应用程序中尝试了它。
$ b 更新:使用
this.getClass()。getClassLoader()。getResourceAsStream(sectionMapping.properties);
而不是
ClassLoader.getSystemResource(sectionMapping.properties)
诀窍!我不知道为什么?
您应该使用 getResourceAsStream
有关如何访问资源,请参见此文章。 。 (这与Glassfish无关 - 它适用于所有Java EE应用服务器。)
更新:如果您的文件位于 src / ss.properties
位置,请检查它是否已复制到 WEB-INF / classes
。然后,您应该可以使用以下代码访问它:
InputStream propStream = ClassLoader.getResourceAsStream(ss.properties );
或(注意如果使用 java.lang.Class中的方法
)
InputStream propStream = Class.getResourceAsStream(/ ss.properties);
请注意,完整的文件名称(包括 如果这两项都不起作用,请将 I'm using a class to get a property file under the source folder. But it doesn't work! After checking, I found that the default path by using is not the web application path but the glassfish config path! What can I do if I want to fetch the property file stored in the "classes" path? Usually the default path is the project path, you know. I've used the Update: Using instead of did the trick! I wonder why? You should use See also this JavaWorld article. Update: If your file is in the location or (note leading slash if using the method in Note that the full file name (including the If neither of these work, please replace the 这篇关于使用GlassFish的WebApplication中的文件路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! .properties $ c
getResourceAsStream
呼叫替换为 getResource(...)。openStream()
并发布应引发的异常的详细信息。File f = new File("/src/ss.properties");
ClassLoader.getResourceAsStream("sss")
.But it returns null! I'm sure the file name is correct because I've tried it in another simple Java app.this.getClass().getClassLoader().getResourceAsStream("sectionMapping.properties");
ClassLoader.getSystemResource("sectionMapping.properties")
getResourceAsStream
, or similar. See this post for how to access resources. (This is independent of Glassfish - it applies to all Java EE app servers.)src/ss.properties
, check that it has been copied to WEB-INF/classes
. Then, you should be able to access it with the following code:InputStream propStream = ClassLoader.getResourceAsStream("ss.properties");
java.lang.Class
)InputStream propStream = Class.getResourceAsStream("/ss.properties");
.properties
extension) needs to be used.getResourceAsStream
call with getResource(...).openStream()
and post details of the exception which should be thrown.