`
rmzdb
  • 浏览: 78631 次
  • 性别: Icon_minigender_1
  • 来自: 合肥
社区版块
存档分类
最新评论

【整】getResource().getPath() 路径带 %20 问题展开

 
阅读更多

getResource().getPath()返回的路径空格变成了 %20

最近我们有一个 web 程序,为了取到配置文件的路径,采用了
this.getClass().getResource(“/”).getPath()
的方法来取得存放 class 的物理路径。本来测试的时候没问题,一发布崩溃掉了。究其原因是发布环境的 Tomcat 的安装目录带有空格,而 getPath 的返回值把空格给转换成了 “%20”。经过一番调查,原来是这是 Java 的一个历史悠久的 bug:

Bug ID: 4466485 getClass( ).getResource( ).getFile( ) returns file name with %20

此 bug 于 2001年6月被提出来,2002年11月最终关闭。
没有修复的原因是这样做会导致兼容性问题。
官方给出的解决方法是采用URI类再把它解码出来。

    URI uri = new URI(url.toString());
    FileInputStream fis = new FileInputStream(uri.getPath());

而这个bug是当初为了修复另一个 bug 的时候引入的:
Bug ID: 4359123 NoClassDefFoundError if ‘#’ anywhere in path

我们最终决定不用这个拐弯抹角的方法,干脆采用下面几种办法直接取得 Stream。

    ClassLoader.getResourceAsStream ("some/pkg/resource.properties");
    Class.getResourceAsStream ("/some/pkg/resource.properties");
    ResourceBundle.getBundle ("some.pkg.resource");
 
Java路径中的空格问题

1, TestURL().class.getResource("").getPath()或TestURL().class.getResource("").getFile()获得的路径,不能被FileReader()和FileWriter()直接应用。
原因是URL对空格,特殊字符(%,#,[]等)和中文进行了编码处理。
例如:空格变为%20。
有解决方法(1),使用repaceAll("%20",' ')替换后,只能解决空格问题。但是路径中包含%和中文就不行了。
有解决方法(2),使用URLDecoder.decode(str,"UTF-8")解码,但是只能解决一部分,若路径中含有+,也是不能解决的,原因是URL并不是完全用URLEncoder.encode(str,"UTF-8")编码的,+号被解码后,却变成了空格。
方法(3),可以解决所有的问题,用TestURL().class.getResource("").toURI().getPath(),但是需要处理URISyntaxException异常,比较麻烦点。

 

java 路径中的空格问题

若路径中带空格,则
1. uri.getpath();返回的路径中的空格仍以“空格”的形式出现,如/F:/MyEclipse Workspace/project/bin/...
除此之外,URL返回的一切路径中的空格都以“%20”的形式出现,uri.toString()也以“%20”的形式出现。

2. new File(String filePath);接受正确URI格式的参数和带“空格”(非20%)的正确相对/绝对字符串路径,否则即使给的路径是正确的也会出现找不到文件的异常。

3. URL/URI返回的路径分隔符都是“/”,File返回的路径分隔符都为“\”。对于存在的文件返回的路径字符串,空格都以"空格"出现,而不存在的路径new出的file,getPath()返回的路径中的空格,仍是new File(String filePath)的参数中原有的形式,即若filePath中是空格的getPath()返回的仍是空格,是“%20”的仍是“%20”。

4.new URL();的参数可以为正确的URI,或者为URI格式的字符串;若字符串是非完整的URI格式,则创建失败。

5.File.toURI()会将file的路径名中的“空格”转化为“%20”,然后在路径前加protocol:"file:/",而File.toURL()只会在file路径前简单的加上protocol:"file:/",而不会将“空格”转化为“%20”,原来的无论是“空格”还是“%20”都只会原样保留!

6.Woden 中WSDLReader.readWSDL(String s)的实现要将参数s转化为URL,所以字符串参数s中一定不能有空格,应以“20%”代替。参数s最好为标准的URI格式的字符串。

 

 

java 路径解决方案

Java的路径问题,相对来说就比较繁杂。最近的工作涉及到创建和读取文件的工作,现将实际使用中遇到的问题总结如下:
一 相对路径的解释
1.相对路径(即相对于当前用户目录的相对路径)均可通过以下方式获得(不论是一般的java项目还是web项目)
String relativelyPath=System.getProperty("user.dir");
对于一般的java项目中的文件是相对于项目的根目录,而对于web项目中的文件路径,可能是服务器的某个路径,同时不同的web服务器也不同(tomcat是相对于 tomcat安装目录\bin)。为此,个人认为,在web项目中,最好不要使用“相对于当前用户目录的相对路径”。然而默认情况下,java.io 包中的类总是根据当前用户目录来分析相对路径名。此目录由系统属性 user.dir 指定,通常是 Java 虚拟机的调用目录。这就是说,在使用java.io包中的类时,最好不要使用相对路径。否则,虽然在SE程序中可能还算正常,但是到了EE程序中,弄不好,就会带来问题一片哦。
2.相对于classpath的相对路径
如:相对于file:/D:/mywork/javaprj/MyTest/bin这个路径的相对路径。其中,bin是本项目的classpath。所有的Java源文件编译后的.class文件复制到这个目录中。
二 类加载目录(即当运行某一类时获得其装载目录)
1.不论是一般的java项目还是web项目,先定位到能看到包路径的第一级目录
InputStream is=ReadWrite.class.getClassLoader().getResourceAsStream("DeviceNO");
其中,DeviceNO文件的路径为 项目名\src\DeviceNO;类ReadWrite所在包的第一级目录位于src目录下。
2.与1相似,不同的是此方法必须以'/'开头
InputStream is=ReadWrite.class.getResourceAsStream("DeviceNO");
其中,DeviceNO文件的路径为 项目名\src\DeviceNO;类ReadWrite所在包的第一级目录位于src目录下。
三. web项目根目录获取
1. 可建立一个servlet,在其init方法中写入如下语句
ServletContext sc=this.getServletContext();
String temp=sc.getRealPath("/");
得到的输出路径结果类似:"D:\Apache\Tomcat6.0\webapps\windpower\ " (windpower为项目名字) ,如果是调用了s1.getRealPath("")则输出"D:\Apache\Tomcat6.0\webapps\windpower"(注意,在最后少了一个"\")
2. 在httpServletRequest中,可以通过下面语句
String cp=request.getSession().getServletContext().getRealPath("/"); 得到的输出路径结果类似:"D:\Apache\Tomcat6.0\webapps\windpower\ "
四 .类路径( classpath)的获取(在Eclipse/MyEclipse中,则为获得src或者classes目录的路径)
方法1. Thread.currentThread().getContextClassLoader().getResource("").getPath()
例如:
String path=Thread.currentThread().getContextClassLoader().getResource("").getPath();
System.out.println(path);
打印:“/D:/windpower/WebRoot/WEB-INF/classes/”
方法2. ParsingXML.class.getClassLoader().getResource("").getPath()(ParsingXML为src某一个包中的类,下同)
例如:
String path=ParsingXML.class.getClassLoader().getResource("").getPath();
System.out.println("ParsingXML.class.getClassLoader().getResource--"+path);
打印: “ParsingXML.class.getClassLoader().getResource--/D:/windpower/WebRoot/WEB-INF/classes/”
另外,如果想把文件放在某一包中,则可以通过以下方式获得到文件所在目录,即先定位到该包的最后一级目录。
ParsingXML.class.getResource("").getPath();
例如:
String path=ParsingXML.class.getResource("").getPath();
System.out.println("ParsingXML.class.getResource---"+p2);
打印: “ParsingXML.class.getResource---/D:/windpower/WebRoot/WEB-INF/classes/parsing/ ”(ParsingXML为src目录下parsing包中的类)
五. 属性文件的读取:
方法1.
static {
ps = new Properties();
try {
InputStream in = ReadWrite.class.getResourceAsStream("DeviceNO");
ps.load(in);
in.close();
} catch (Exception e) {
e.printStackTrace();
}
ps.getProperty("key")
方法2.
Locale locale = Locale.getDefault();
ResourceBundle localResource = ResourceBundle.getBundle("windpower/DeviceNOProperties", locale);
String value = localResource.getString("1");
System.out.println("DeviceNO: " + value);
工程src目录下文件DeviceNOProperties.properties(名字后缀必须为properties)文件内容如下:1=3输出结果为:“DeviceNO:3”
六.编码转换问题:
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 路径p source 等等。这里我们只要在获取到路径之前把返回值decode下就可以了. 用utf-8编码. Java代码 :
String configPath = this.getClass().getClassLoader().getResource("allowPath.xml").getFile();
configPath = java.net.URLDecoder.decode(configPath,"utf-8");
另外java中URL 的编码和解码函数java.net.URLEncoder.encode(String s)和java.net.URLDecoder.decode(String s);在javascript 中URL 的编码和解码函数escape(String s)和unescape(String s) ;
七.总结:
我们在使用相对路径时,应当使用相对于当前classpath的相对路径。
ClassLoader类的getResource(String name),getResourceAsStream(String name)等方法,使用相对于当前项目的classpath的相对路径来查找资源。
读取属性文件常用到的ResourceBundle类的getBundle(String path)也是如此。
通过查看ClassLoader类及其相关类的源代码,发现它实际上还是使用了URI形式的绝对路径。通过得到当前classpath的URI形式的绝对路径,再去构建相对路径的URI形式的绝对路径。

分享到:
评论

相关推荐

    Java路径问题解决方案汇集

     1、 URLTest.class.getResource(/).getPath();  URLTest.class.getResource(/).getFile();  URLTest.class.getClassLoader().getResource().getPath();  Thread.currentThread()....

    getpath 抓路径

    getpath 抓路径getpath 抓路径getpath 抓路径

    java类所在工程路源代码

    java类所在工程路源代码String path=this.getClass().getResource("/").getPath(); xmlpath=xmlpath.replaceAll( "%20"," ");

    关于laydate.js加载laydate.css路径错误问题解决

    laydate.js是通过ocLazyLoad异步加载引入的,结果始终加载不出来laydate.css文件,看了下路径错误,于是扒开代码发现是这样写的: getPath:function(){ var e=document.scripts, t=e[e.length-1], n=t.src;

    百度地图API

    // 自行添加起点和终点 var start = results.getStart(); var end = results.getEnd(); addStart(start.point, start.title); addEnd(end.point, end.... viewPoints.concat(plan.getRoute(i).getPath()); }

    getPath.html

    getPath.html

    java 文件处理代码

    System.out.println("最早的一个文件的路径-->"+tm.get(tm.firstKey()).getPath()+tm.firstKey()); System.out.println("最近的一个文件的路径-->"+tm.get(tm.lastKey()).getPath()+tm.lastKey()); Set<Long>...

    JAVA获取文件绝对路径的方法

    主要介绍了JAVA获取文件绝对路径的方法,涉及针对文件路径的操作技巧,需要的朋友可以参考下

    应用移植WebSphere问题列表(经验是买不到的)

    在websphere通过getClassLoader().getResource("").getPath()取配置文件的路径跟在tomcat环境下是不一样的,如果配置文件是放在classes目录下,会报找不到配置文件。 发布应用时,报web.xml文件有错 发布应用成功,...

    android获取图片尺寸的两种方式及bitmap的缩放操作

    String path=uri.getPath(); Log.e(图片路径,path+); SpannableString spannableString=new SpannableString(path); //方法一:通过uri把图片转化为bitmap的方法 Bitmap bitmap= BitmapFactory.decodeFile(...

    snappool数据库连接池的应用

    URL url = DBConnectionPoolSnap.class.getResource(JDBC_CONNECTION_POOL_CONFIG_FILE); String path = url.getPath(); File file = new File(path); ConnectionManager.init(file.getPath()); } ...... ...

    GetPath 1.23.031.rar

    GetPath 1.23.031.rar 老版本的 ,速度很快的说。。扫目录很好啊啊~~

    1648238771053337_getpath.do

    1648238771053337_getpath.do

    Android开发email

    通过第三方库开发Email,内包含activation.jar、additionnal.jar....getPath() + "/test1.txt") .addFile(Environment.getExternalStorageDirectory().getPath() + "/test2.ini") .create(); email.send();

    java压缩文件源码--ZipUtils

    // entry以相对路径的形式设置。以文件夹C:\temp例如temp\test.doc或者test.xls 如果设置不当,会出现拒绝访问等错误 // 分别处理单个文件/目录的entry if(rootStr.equals(tempFile.getPath())){ ...

    Java获取*路径实现探讨

    (1)、request.getRealPath(“/”);//不推荐使用获取工程的根路径 (2)、...//获取工程的根路径,这个方法比较好用,可以直接在servlet和jsp中使用 (4)、 this.getClass().getClassLoader().getResource(“”).getPath();

    getpath.do

    getpath.do

    jQuery-plugin-getPath:jQuert getPath - 查找 HTML 元素的(可能的)jQuery 选择器

    用法 $('body').bind('click.monitoring',function(e){//Get the element's jQuery selector//It'll return something like this: #example-5>h4:eq(0)var sel = $.getPath(e.target); //is the same as $(e.target)...

    Web安全测试软件GetPath

    Web安全测试软件GetPath Web安全测试软件GetPath

    jsp结合javabean的实践

    Jsp结合JavaBean的实践(用到HashMap) javaBean的用处主要在于存取页面信息,通过getXXX和setXXX方法实现。 1、声明javaBean ... Id:是类的代号 ... public String getPath(){ return path; } }

Global site tag (gtag.js) - Google Analytics