How to get files from resources folder. Spring Framework
Asked Answered
B

3

23

I'm trying to unmarshal my xml file:

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)

Here is my structure:

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());

I can see an error more clearly:

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

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

enter image description here

Burdick answered 4/4, 2016 at 16:5 Comment(4)
try getResource("classpath:xmlToParse/companies.xml")Polymerism
Wont you need another "/" before xmlToParseAnstice
Code was update. Check it out, please.Burdick
any resolve? I face the same problemOctavo
G
47

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.

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:

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

To obtain the needed InputStream, just call

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

Gebler answered 13/9, 2016 at 14:28 Comment(2)
I tried this, works locally but cannot be resolve when deploy to dev. https://mcmap.net/q/261116/-filenotfoundexception-in-spring-boot-jar-but-the-file-is-present/5777189Calculus
It doesn't work even locallyOctavo
D
13
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("fileName").getFile());
Dehiscence answered 7/4, 2017 at 2:58 Comment(1)
This break as soon as you deploy this spring project into a jar. See https://mcmap.net/q/82936/-reading-a-resource-file-from-within-jar/5777189Calculus
D
-2

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();
        }
    }
}
Dewittdewlap answered 4/4, 2016 at 16:15 Comment(2)
Check whether the resources-folder is included in the deployment-assemblyDewittdewlap
I think, I have to bind this folder... Is there any easy way to do it?Burdick

© 2022 - 2024 — McMap. All rights reserved.