如何避免从URL.getFile()获取URL编码的路径? [英] How to avoid getting URL encoded paths from URL.getFile()?
本文介绍了如何避免从URL.getFile()获取URL编码的路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
尝试获取给定资源的路径时遇到以下问题:
I'm having the following problem when trying to get the path of a given resource:
System.out.println("nf="+new File(".").getAbsolutePath());
System.out.println("od="+new File(this.getClass().getResource(".").getFile());
我得到的输出是:
nf=C:\Users\current user\workspace\xyz\.
od=C:\Users\current%20user\workspace\xyz\bin\something
问题在于%20 URL编码.如何避免呢?是否有一种直接的方法可以避免一开始就得到这种字符串,还是应该只对返回URL的某些方法运行返回的字符串?
The problem lies with the %20 URL encoding thing. How to avoid it? Is there a direct way to avoid getting this kind of string in the first place, or should I just run the returned string against some method that will do the URL decoding?
谢谢
推荐答案
这归因于API中的URL处理问题.您可以通过以下方法解决此问题:首先将URL字符串转换为URI :
This is due to a URL handling quirk in the API. You can work around this by converting the URL string to a URI first:
new URI(this.getClass().getResource(".").toString()).getPath()
这将产生如下字符串:
"C:\Users\current user\workspace\xyz\bin\something"
这篇关于如何避免从URL.getFile()获取URL编码的路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文