Java 如何从资源文件夹中获取文件。弹簧框架

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36407575/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 17:50:52  来源:igfitidea点击:

How to get files from resources folder. Spring Framework

javaspring-mvcjava-iofileinputstream

提问by Tom Wally

I'm trying to unmarshal my xml file:

我正在尝试解组我的 xml 文件:

public Object convertFromXMLToObject(String xmlfile) throws IOException {
    FileInputStream is = null;
    File file = new File(String.valueOf(this.getClass().getResource("xmlToParse/companies.xml")));
    try {
        is = new FileInputStream(file);
        return getUnmarshaller().unmarshal(new StreamSource(is));
    } finally {
        if (is != null) {
            is.close();
        }
    }
}

But I get this errors: java.io.FileNotFoundException: null (No such file or directory)

但我收到此错误: java.io.FileNotFoundException: null (No such file or directory)

Here is my structure:

这是我的结构:

enter image description here

enter image description here

Why I can't get files from resources folder? Thanks.

为什么我无法从资源文件夹中获取文件?谢谢。

Update.

更新。

After refactoring,

重构后,

URL url = this.getClass().getResource("/xmlToParse/companies.xml"); File file = new File(url.getPath());

URL url = this.getClass().getResource("/xmlToParse/companies.xml"); File file = new File(url.getPath());

I can see an error more clearly:

我可以更清楚地看到错误:

java.io.FileNotFoundException: /content/ROOT.war/WEB-INF/classes/xmlToParse/companies.xml (No such file or directory)

java.io.FileNotFoundException: /content/ROOT.war/WEB-INF/classes/xmlToParse/companies.xml(没有这样的文件或目录)

It tries to find WEB-INF/classes/ I have added folder there, but still get this error :(

它试图找到 WEB-INF/classes/ 我在那里添加了文件夹,但仍然出现此错误:(

enter image description here

enter image description here

回答by Raphael Roth

you are suppose to give an absolute path (so add a loading ′/′, where resource-folder is the root-folder):

你应该给出一个绝对路径(所以添加一个加载'/',其中资源文件夹是根文件夹):

public Object convertFromXMLToObject(String xmlfile) throws IOException {
    FileInputStream is = null;
    File file = new File(String.valueOf(this.getClass().getResource("/xmlToParse/companies.xml")));
    try {
        is = new FileInputStream(file);
        return getUnmarshaller().unmarshal(new StreamSource(is));
    } finally {
        if (is != null) {
            is.close();
        }
    }
}

回答by jonashackt

I had the same problem trying to load some XML files into my test classes. If you use Spring, as one can suggest from your question, the easiest way is to use org.springframework.core.io.Resource- the one Raphael Roth already mentioned.

我在尝试将一些 XML 文件加载到我的测试类中时遇到了同样的问题。如果您使用 Spring,正如您的问题所建议的那样,最简单的方法是使用org.springframework.core.io.Resource- Raphael Roth 已经提到的那个。

The code is really straight forward. Just declare a field of the type org.springframework.core.io.Resourceand annotate it with org.springframework.beans.factory.annotation.Value- like that:

代码真的很简单。只需声明一个org.springframework.core.io.Resource类型的字段并使用org.springframework.beans.factory.annotation.Value对其进行注释- 就像这样:

@Value(value = "classpath:xmlToParse/companies.xml")
private Resource companiesXml;

To obtain the needed InputStream, just call

要获取所需的 InputStream,只需调用

companiesXml.getInputStream()

and you should be okay :)

你应该没事:)

But forgive me, I have to ask one thing: Why do you want to implement a XML parser with the help of Spring? There are plenty build in :) E.g. for web services there are very good solutions that marshall your XMLs into Java Objects and back...

但是原谅我,我要问一件事:为什么要借助Spring来实现XML解析器?有很多内置:) 例如,对于 Web 服务,有非常好的解决方案可以将您的 XML 编组为 Java 对象并返回......

回答by bearwithbeard

ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("fileName").getFile());