java - 如何避免从 URL.getFile() 获取 URL 编码路径?

标签 java url-encoding

我在尝试获取给定资源的路径时遇到以下问题:

    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 解码的某些方法运行返回的字符串?

谢谢

最佳答案

这是由于 API 中的 URL 处理怪癖。您可以通过首先将 URL 字符串转换为 URI 来解决此问题:

new URI(this.getClass().getResource(".").toString()).getPath()

这将产生如下字符串:

"C:\Users\current user\workspace\xyz\bin\something"

关于java - 如何避免从 URL.getFile() 获取 URL 编码路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8928661/

相关文章:

用于提取货币短语的 Java 正则表达式

java - JSP 无法解析 org.apache.commons.lang 的导入

angularjs - Angular 编码 url 搜索字符串

java - 强制子类中的 toString() 实现

asp.net - 分配 location.href 时,请说明 url 编码(在 asp.net 和 firefox 中)

android - Retrofit2 替换查询参数

.htaccess - mod-rewrite 转发而不改变 URL

java - 将数据从Android插入PHP文件,然后插入Mysql错误

java - 数字溢出抛出异常

java - ActionListener java 遇到问题