How to get the path to file in Spring Boot to use it as an argument in another library method?

Viewed 1469

I need a way to get a resource path in Spring Boot because I will use this path as an argument to another method in existing library, which I cannot change.

It is used as:

FileInputStream fis = new FileInputStream(fileName);

I have to provide that filename there. The resource in a Spring Boot application is under src/main/resources folder. Using Resource and getResource() does not help, as I need to provide a String of a file path.

Using just "src/main/resources/filename" does not work too.

So, my question is: how do you get the path of the files that are in the Spring Boot classpath?

I have solved it this way:

        try {
            filename = ResourceUtils.getURL("classpath:").getPath()+ "myFileName";
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

With this method, when I run an application, it is working, but when I run the jar, then I get an exception:

(The filename, directory name, or volume label syntax is incorrect)

Did anyone get this issue? Please help

0 Answers
Related