1

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:

  • src

    • main

      • java

        • com.app
          • controllers
          • models
          • repositories <- I am calling from here
      • resources

        • static
          • Test.xlsx <- I'm trying to access this

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.

Ibn Masood
  • 1,093
  • 1
  • 14
  • 31
  • 1
    suppose this [post](http://stackoverflow.com/questions/36371748/spring-boot-access-static-resources-missing-scr-main-resources) may have the solution for you. – Rajith Pemabandu Apr 16 '17 at 05:27
  • @RajithPemabandu I have tried the proposed solution from that post but it is still not found – Ibn Masood Apr 16 '17 at 05:29

2 Answers2

1

I believe the correct in your case would be

new ClassPathResource("/static/Test.xlsx").getFile()
Nikem
  • 5,716
  • 3
  • 32
  • 59
0

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.

Rajith Pemabandu
  • 616
  • 7
  • 14
  • I'm not using Spring MVC I just have simple setup. Can this also be achieved by adding "spring.mvc.static-path-pattern=/resources/**" to the application.properties? – Ibn Masood Apr 16 '17 at 05:51