org.springframework.core.io.ResourceLoader
是spring用于加载资源的重要机制,例如查找配置文,属性文件等资源,下面说一下怎么在我们的项目中需要的时候使用他帮助我们简化资源的访问,而不用写很繁琐的代码访问资源,还存在jar和war中不能访问的问题。
private File obtainResourceAsFile(String relativePath) {
ResourceLoader resourceLoader = new DefaultResourceLoader();
Resource resource = resourceLoader.getResource(relativePath);
try {
return resource.getFile();
} catch (IOException e) {
logger.error("The file cannot be obtained by resource loader, may be it's" +
" not exists, make sure it's exists please. "
+ e.getLocalizedMessage());
return null;
}
}
网友评论