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.

2
  • 1
    suppose this post may have the solution for you. Commented Apr 16, 2017 at 5:27
  • @RajithPemabandu I have tried the proposed solution from that post but it is still not found Commented Apr 16, 2017 at 5:29

2 Answers 2

1

I believe the correct in your case would be

new ClassPathResource("/static/Test.xlsx").getFile() 
Sign up to request clarification or add additional context in comments.

Comments

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.

1 Comment

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?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.