java java中的sysLoader.getResource()问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3263560/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
sysLoader.getResource() problem in java
提问by Vishal
I am having following lines of code.
我有以下几行代码。
sysLoader = (URLClassLoader)Thread.currentThread().getContextClassLoader();
url = sysLoader.getResource("tempFile.txt");
It is giving an weird problem. If I run this from a path where there is no space in the path (Folder names) then it is running fine. But if the path contains any spaces (line "c:\New Foler...") then it is not working.
它给出了一个奇怪的问题。如果我从路径(文件夹名称)中没有空格的路径运行它,那么它运行良好。但是,如果路径包含任何空格(行“c:\New Foler...”),则它不起作用。
How to solve this?
如何解决这个问题?
EDIT: In more detail - I inspected the sysloader object.
编辑:更详细地 - 我检查了 sysloader 对象。
sysloader -> UCP -> path
sysloader -> UCP -> 路径
Is having a path with character %20 instead of space
有一个带有字符 %20 而不是空格的路径
And therefore all the URLs are null.
因此所有的 URL 都是空的。
How to resolve this?
如何解决这个问题?
回答by snoopygee
This is known by Sun/Oracle, their advice is to use URI objects which will remove the %20 characters:
Sun/Oracle 知道这一点,他们的建议是使用 URI 对象来删除 %20 字符:
Instead of doing this:
而不是这样做:
FileInputStream fis = new FileInputStream(url.getFile());
you can force any %-escaped characters to be decoded by first converting the URL to a URI, and then use the path component of the URI as the filename:
您可以通过首先将 URL 转换为 URI,然后使用 URI 的路径组件作为文件名来强制解码任何 % 转义字符:
URI uri = new URI(url.toString());
FileInputStream fis = new FileInputStream(uri.getPath());
回答by libor
Use URLDecoder.decode()method to replace %20characters by spaces.
使用URLDecoder.decode()方法%20用空格替换字符。
String path = URLDecoder.decode(url.getPath(), "UTF-8");
String path = URLDecoder.decode(url.getPath(), "UTF-8");
Please also keep in mind that when resource is located in jar file you have to handle it different way. See it e.g. here: How to access resources in jar where it can be present in multiple jar
还请记住,当资源位于 jar 文件中时,您必须以不同的方式处理它。例如在这里看到它:How to access resources in jar where it can be present in multiple jar
回答by Nitin Labhishetty
To get the URL of the file from string, when the path contains spaces, this is what worked for me:
要从字符串中获取文件的 URL,当路径包含空格时,这对我有用:
File file = new File("/Users/work space/tempFile.txt");
URL url = file.toURI().toURL();
According to Javadocs, file.toURL()is deprecated:
根据 Javadocs,file.toURL()已弃用:
This method does not automatically escape characters that are illegal in URLs. It is recommended that new code convert an abstract pathname into a URL by first converting it into a URI, via the toURI method, and then converting the URI into a URL via the URI.toURL method.
此方法不会自动转义 URL 中的非法字符。建议新代码将抽象路径名转换为 URL,首先通过 toURI 方法将其转换为 URI,然后通过 URI.toURL 方法将 URI 转换为 URL。
Hence used file.toURI().toURL().
因此使用file.toURI().toURL()。
For Java 7+, this is approach can be taken instead:
对于 Java 7+,可以采用这种方法:
URL url = Paths.get("/Users/work space/tempFile.txt").toURI().toURL());
Note: If the path begins with a /it is considered absolute else taken as a relative path.
注意:如果路径以 a 开头,/则将其视为绝对路径,否则将视为相对路径。

