getresource 路径转义_关于Class.getResource和ClassLoader.getResource的路径问题(转)

参考博客:http://www.cnblogs.com/yejg1212/p/3270152.html

Class.getResource(String path)

当path以/开头,如/a/b/c.properties时,查找的路径为: classpath路径:path。

当path不以/开头时,如c.properties,查找的路径为:当前调用类的路径:path。

什么意思呢?看下面这段代码的输出结果就明白了:

packagetestpackage;public classTestMain {public static voidmain(String[] args) {

System.out.println(TestMain.class.getResource(""));

System.out.println(TestMain.class.getResource("/"));

}

}

输出结果:

file:/E:/workspace/Test/bin/testpackage/file:/E:/workspace/Test/bin/

※Class.getResource和Class.getResourceAsStream在使用时,路径选择上是一样的。

Class.getClassLoader().getResource(String path)

path不能以’/‘开头时;

path是从ClassPath根下获取;

packagetestpackage;public classTestMain {public static voidmain(String[] args) {

TestMain t= newTestMain();

System.out.println(t.getClass());

System.out.println(t.getClass().getClassLoader());

System.out.println(t.getClass().getClassLoader().getResource(""));

System.out.println(t.getClass().getClassLoader().getResource("/"));//null

}

}//输出结果:

classtestpackage.TestMain

[email protected]

file:/E:/workspace/Test/bin/

null

※Class.getClassLoader().getResource和Class.getClassLoader().getResourceAsStream在使用时,路径选择上也是一样的。

但是我做的实验下面:getClassLoader().getResource("/")也是行的,为什么呢?

在TestController中想要获取com/cy/config包下面的config.xml文件:

@Controllerpublic classTestController {private static Logger logger = (Logger) LoggerFactory.getLogger(TestController.class);

@RequestMapping("/test")public voidtest(HttpServletRequest request){

String path= this.getClass().getResource("/").getPath() + "com"+File.separator+"cy"+File.separator+"config"+File.separator+"config.xml";

System.out.println(path);///D:/tomcat8.5.9/webapps/springMVC/WEB-INF/classes/com\cy\config\config.xml

String path2= this.getClass().getResource("").getPath() + "com"+File.separator+"cy"+File.separator+"config"+File.separator+"config.xml";

System.out.println(path2);///D:/tomcat8.5.9/webapps/springMVC/WEB-INF/classes/com/cy/controller/com\cy\config\config.xml

String path3= this.getClass().getClassLoader().getResource("/").getPath() + "com"+File.separator+"cy"+File.separator+"config"+File.separator+"config.xml";

System.out.println(path3);///D:/tomcat8.5.9/webapps/springMVC/WEB-INF/classes/com\cy\config\config.xml

String path4= this.getClass().getClassLoader().getResource("").getPath() + "com"+File.separator+"cy"+File.separator+"config"+File.separator+"config.xml";

System.out.println(path4);///D:/tomcat8.5.9/webapps/springMVC/WEB-INF/classes/com\cy\config\config.xml

}

}

注意的是:

1.

this.getClass().getClassLoader().getResource("").getPath()中如果文件的路径中有#空格等特殊字符的话,会转义为%23等这样的字符,得到的路径就不正确

这时候要将特殊字符转义,这是我在项目中遇到的:

URLDecoder.decode(path, "UTF-8")

2.File.separator:

File.separator: 是系统分隔符,比如windows下面文件路径的分隔符是"\"; linux下面是"/"

但是在windows下面和linux下面都可以用"/"

原文:http://www.cnblogs.com/tenWood/p/6838966.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值