上代码
public class TestClassPath {
public static void main(String[] args) throws IOException{
Resource r = new ClassPathResource("",TestClassPath.class);
InputStream in = r.getInputStream();
System.out.println(r.getURL());
}
}
1.如果这样的写法,在构造方法中传入该类的class文件,注意这里的路径加了/
获取到的会是
file:/Users/XXX/Documents/Myeclipse10/workspace/Springmvc/WebRoot/WEB-INF/classes/
这样的项目根路径
2.如果将路径中的/去掉则会去当前类的当前路径中寻找
获取到的会是
file:/Users/XXX/Documents/Myeclipse10/workspace/Springmvc/WebRoot/WEB-INF/classes/cn/xxx/test/
3.如果用该类的类加载器作为参数
Resource r = new ClassPathResource("",TestClassPath.class.getClassLoader());
获取到的会是
file:/Users/XXX/Documents/Myeclipse10/workspace/Springmvc/WebRoot/WEB-INF/classes/