Ibn Masood
Ibn Masood

Reputation: 1113

Spring Boot Unable to find File using ClassPathResource

Using Spring Boot. I am trying to parse an excel file stored locally within the project using Apache POI library. My directory structure is like so:


File excel = new ClassPathResource("Test.xlsx").getFile();

File file = new File(classLoader.getResource("src/main/resources/static/test.xlsx").getFile());

I have tried both of the above ways to access the file, and I have tried many variations of paths to the file, but nothing has worked. I do not get build errors or runtime errors, the file is simply not found and when I attempt to invoke the method I get a NullPointerException on that line.

Upvotes: 1

Views: 2717

Answers (2)

Nikem
Nikem

Reputation: 5776

I believe the correct in your case would be

new ClassPathResource("/static/Test.xlsx").getFile()

Upvotes: 1

Rajith Pemabandu
Rajith Pemabandu

Reputation: 616

supoose you just override the addResourceHandlers method of @Configuration class that extends WebMvcConfigurerAdapter

@Override
public void addResourceHandlers(final ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}

this is a blog post about Spring resources - here.

Take look at spring documentation for more details.

Upvotes: 0

Related Questions