Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2545484
  • 博文数量: 320
  • 博客积分: 9650
  • 博客等级: 中将
  • 技术积分: 3886
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-27 21:05
文章分类

全部博文(320)

文章存档

2024年(1)

2017年(5)

2016年(10)

2015年(3)

2014年(3)

2013年(10)

2012年(26)

2011年(67)

2010年(186)

2009年(9)

分类: Java

2010-05-10 11:51:49

 ClassLoader的getResource方法使用了utf-8对路径信息进行了编码,当路径中存在中文和空格时,他会对这些字符进行转换,这样,得到的往往不是我们想要的真实路径,在此,调用了URLDecoder的decode方法进行解码,以便得到原始的中文及空格路径

例如:结果是file:/C:/Documents%20and%20Settings/%e5%ba%84%e6%99%93%e6%af%85  
  /Local%20Settings/Temp/temp0.jar!/db/dmozdata.mdb  

而我们期望是 C:/Documents andsettigsd sdfsdfsdf sdfsdf sdfsd 等等

这里我们只要在获取到的例如: String configPath = this.getClass().getClassLoader().getResource("allowPath.xml").getFile();

把返回前decode下就可以了. 用utf-8编码. 

Java代码
  1. configPath = java.net.URLDecoder.decode(configPath,"utf-8"); 
阅读(1397) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~