从类路径资源(XML 文件)获取输入流

发布于 2024-07-18 05:55:46 字数 91 浏览 4 评论 0原文

在Java Web应用程序中,假设如果我想获取放置在CLASSPATH(即sources文件夹内)中的XML文件的InputStream,我该怎么做?

In Java web application, Suppose if I want to get the InputStream of an XML file, which is placed in the CLASSPATH (i.e. inside the sources folder), how do I do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

伴梦长久 2024-07-25 05:55:46

类加载器.getResourceAsStream()

正如下面评论中所述,如果您处于多ClassLoader环境中(例如单元测试、webapps等),您可能需要使用Thread.currentThread().getContextClassLoader( )。 请参阅http://stackoverflow.com/questions/2308188/getresourceasstream-vs-fileinputstream/2308388#comment21307593_2308388

ClassLoader.getResourceAsStream().

As stated in the comment below, if you are in a multi-ClassLoader environment (such as unit testing, webapps, etc.) you may need to use Thread.currentThread().getContextClassLoader(). See http://stackoverflow.com/questions/2308188/getresourceasstream-vs-fileinputstream/2308388#comment21307593_2308388.

情绪失控 2024-07-25 05:55:46
ClassLoader.class.getResourceAsStream("/path/file.ext");
ClassLoader.class.getResourceAsStream("/path/file.ext");
┈┾☆殇 2024-07-25 05:55:46

这取决于 XML 文件的具体位置。 它是在源文件夹中(在“默认包”或“根”中)还是在与类相同的文件夹中?

对于前一种情况,您必须使用“/file.xml”(注意前导斜杠)来查找文件,并且使用哪个类来尝试查找它并不重要。

如果 XML 文件位于某个类旁边,则可以使用仅包含文件名的 SomeClass.class.getResourceAsStream()

That depends on where exactly the XML file is. Is it in the sources folder (in the "default package" or the "root") or in the same folder as the class?

In for former case, you must use "/file.xml" (note the leading slash) to find the file and it doesn't matter which class you use to try to locate it.

If the XML file is next to some class, SomeClass.class.getResourceAsStream() with just the filename is the way to go.

牵强ㄟ 2024-07-25 05:55:46

ClassLoader.class.getResourceAsStream("/path/to/your/xml") 并确保您的编译脚本将 xml 文件复制到 CLASSPATH 中的位置。

ClassLoader.class.getResourceAsStream("/path/to/your/xml") and make sure that your compile script is copying the xml file to where in your CLASSPATH.

oО清风挽发oО 2024-07-25 05:55:46

someClassWithinYourSourceDir.getClass().getResourceAsStream();

someClassWithinYourSourceDir.getClass().getResourceAsStream();

哭泣的笑容 2024-07-25 05:55:46

这个答案中的一些“getResourceAsStream()”选项对我不起作用,但这个选项对我有用:

SomeClassWithinYourSourceDir.class.getClassLoader().getResourceAsStream("yourResource");

Some of the "getResourceAsStream()" options in this answer didn't work for me, but this one did:

SomeClassWithinYourSourceDir.class.getClassLoader().getResourceAsStream("yourResource");

空城之時有危險 2024-07-25 05:55:46

我尝试了建议的解决方案,但文件名中的正斜杠对我不起作用,例如: ...().getResourceAsStream("/my.properties"); 返回 null

删除斜杠有效: ....getResourceAsStream("my.properties");

这是来自文档API:
在委派之前,使用以下算法根据给定资源名称构造绝对资源名称:

If the name begins with a '/' ('\u002f'), then the absolute name of the resource is the portion of the name following the '/'.
Otherwise, the absolute name is of the following form:

    modified_package_name/name 

Where the modified_package_name is the package name of this object with '/' substituted for '.' ('\u002e'). 

I tried proposed solution and forward slash in the file name did not work for me, example: ...().getResourceAsStream("/my.properties"); null was returned

Removing the slash worked: ....getResourceAsStream("my.properties");

Here is from doc API:
Before delegation, an absolute resource name is constructed from the given resource name using this algorithm:

If the name begins with a '/' ('\u002f'), then the absolute name of the resource is the portion of the name following the '/'.
Otherwise, the absolute name is of the following form:

    modified_package_name/name 

Where the modified_package_name is the package name of this object with '/' substituted for '.' ('\u002e'). 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文