简体   繁体   English

如何从资源文件夹中获取文件。 Spring框架

[英]How to get files from resources folder. Spring Framework

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(没有这样的文件或目录)

Here is my structure: 这是我的结构:

在此输入图像描述

Why I can't get files from resources folder? 为什么我无法从资源文件夹中获取文件? Thanks. 谢谢。

Update. 更新。

After refactoring, 重构后,

URL url = this.getClass().getResource("/xmlToParse/companies.xml"); URL url = this.getClass()。getResource(“/ xmlToParse / companies.xml”); File file = new File(url.getPath()); 文件文件=新文件(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 /我在那里添加了文件夹,但仍然收到此错误:(

在此输入图像描述

I had the same problem trying to load some XML files into my test classes. 尝试将一些XML文件加载到我的测试类中时遇到了同样的问题。 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 Rot h already mentioned. 如果您使用Spring,可以从您的问题中建议,最简单的方法是使用org.springframework.core.io.Resource--一个Raphael Rot h已经提到过。

The code is really straight forward. 代码非常简单。 Just declare a field of the type org.springframework.core.io.Resource and 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? 但是请原谅我,我要问一件事:你为什么要在Spring的帮助下实现XML解析器? There are plenty build in :) Eg for web services there are very good solutions that marshall your XMLs into Java Objects and back... 有很多内置:)例如,对于Web服务,有非常好的解决方案可以将您的XML编组到Java对象中并返回...

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

you are suppose to give an absolute path (so add a loading ´/´, where resource-folder is the root-folder): 你假设给出一个绝对路径(所以添加一个加载'/',其中resource-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();
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Eclipse中,Junit不从src / test / resources文件夹中拾取文件。 - In Eclipse, Junit is not picking up files from src/test/resources folder. 如何在Spring Rest中的资源文件夹中上传文件 - How to upload files in resources folder in spring rest 如何从java资源文件夹内的文件夹中获取所有文件的列表 - how to get list of all files from folder inside java resources folder 如何将文件夹的所有文件加载到 Spring 中的资源列表中? - How to load all files of a folder to a list of Resources in Spring? 在 Spring 引导应用程序中从 JAR 上的资源文件夹中获取图像 - Get images from resources folder on a JAR in a Spring boot application JSP页面无法从资源文件夹-IntelliJ获取Bootstrap文件 - JSP page is not able to get the Bootstrap files from the resources folder -IntelliJ 如何告诉spring从外部文件夹加载资源? - how to tell spring to load resources from external folder? 从文件夹中检索图像,缩小它们,将它们保存到另一个文件夹中。 怎么样? (JAVA) - Retrieve images from a folder, Scale down them, save them into another folder. How? (Java) 在 Spring Boot 中从 resources 文件夹中读取文件 - Read file from resources folder in Spring Boot 从资源文件夹 JAVA 中获取文件夹 - Get folder from Resources folder JAVA
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM