使用GlassFish的WebApplication中的文件路径 [英] file path in WebApplication using GlassFish

查看:88
本文介绍了使用GlassFish的WebApplication中的文件路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个类来获取源文件夹下的属性文件。但它不起作用!检查后,我发现使用

  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应用服务器。)



另见本篇JavaWorld文章



更新:如果您的文件位于 src / ss.properties 位置,请检查它是否已复制到 WEB-INF / classes 。然后,您应该可以使用以下代码访问它:

  InputStream propStream = ClassLoader.getResourceAsStream(ss.properties ); 

或(注意如果使用 java.lang.Class中的方法

  InputStream propStream = Class.getResourceAsStream(/ ss.properties); 

请注意,完整的文件名称(包括 .properties

如果这两项都不起作用,请将 getResourceAsStream 呼叫替换为 getResource(...)。openStream()并发布应引发的异常的详细信息。


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

File f = new File("/src/ss.properties");

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 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.

Update: Using

this.getClass().getClassLoader().getResourceAsStream("sectionMapping.properties");

instead of

ClassLoader.getSystemResource("sectionMapping.properties")

did the trick! I wonder why?

解决方案

You should use getResourceAsStream, or similar. See this post for how to access resources. (This is independent of Glassfish - it applies to all Java EE app servers.)

See also this JavaWorld article.

Update: If your file is in the location 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");

or (note leading slash if using the method in java.lang.Class)

InputStream propStream = Class.getResourceAsStream("/ss.properties");

Note that the full file name (including the .properties extension) needs to be used.

If neither of these work, please replace the getResourceAsStream call with getResource(...).openStream() and post details of the exception which should be thrown.

这篇关于使用GlassFish的WebApplication中的文件路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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