Monday 7 November 2011

Spring: how to locate a file in WEB-INF/classes/resources

The project if built with Spring and has the standard filesystem of Maven.


Assume we want to read foo.txt in WEB-INF/classes/resources/foo.txt


ClassPathResource r = new ClassPathResource("foo.xml");
try {
System.out.println("full path"+r.getFile().getAbsolutePath());
} catch (IOException e1) {
e1.printStackTrace();
}

The output is the full path to foo.xml.

References
http://static.springsource.org/spring/docs/current/spring-framework-reference/html/resources.html#resources-implementations-classpathresource

No comments:

Post a Comment