Spring MVC : read file from src/main/resources

Resource resource = new ClassPathResource(fileLocationInClasspath);
InputStream resourceInputStream = resource.getInputStream();

using ClassPathResource and interface resource. But make sure you are copying the resources directory correctly (using maven), and its not missing, for example if running tests as part of test context.


Here is one way of loading classpath resources.

Resource resource = applicationContext.getResource("classpath:xyz.xml");
InputStream is = resource.getInputStream();

For spring based application you can take advantage of ResourceUtils class.

File file = ResourceUtils.getFile("classpath:xyz.xml")

Tags:

Spring Mvc