乱码解决

有时在 Java 中获取文件路径时,如果路径中存在一些特殊字符,如 %、& 符号等,或者是存在中文,亦或是存在空格的时候,路径就有可能出现乱码
解决方案有以下几种:
在获取到路径后,进行一次编码

String path = this.getClass().getClassLoader().getResource("文件路径").getPath().substring(1);

path = java.net.URLDecoder.decode(path, "utf-8");

调用 getResource() 方法后再调用 toURI() 方法,注意此时的返回值类型为 URI,而不是 URL,可以参照这里

URI uri = classLoader.getResource("文件路径").toURI();
String path = uri.getPath();

原文地址:https://www.cnblogs.com/yeyeshenghui/p/10951324.html