Java:如何从转义的 URL 中获取文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2166039/
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
Java: how to get a File from an escaped URL?
提问by SyntaxT3rr0r
I'm receiving an URL that locates a local file (the fact that I receive an URL is not in my control). The URL is escaped validly as defined in RFC2396. How can I transform this to a Java File object?
我收到了一个定位本地文件的 URL(我收到的 URL 不在我的控制范围内)。URL 按照 RFC2396 中的定义进行有效转义。如何将其转换为 Java File 对象?
Funnily enough, the URL getFile() method returns a String, not a File.
有趣的是,URL getFile() 方法返回一个字符串,而不是一个文件。
I've created a directory called "/tmp/some dir" (with a spacing character between "some" and "dir"), which is correctly located by the following URL: "file:///tmp/some%20dir" (quotes added for clarity).
我创建了一个名为“/tmp/some dir”的目录(“some”和“dir”之间有一个空格字符),它可以通过以下 URL 正确定位:“file:///tmp/some%20dir” (为清楚起见添加了引号)。
How can I convert that URL to a Java File?
如何将该 URL 转换为 Java 文件?
To give more detail about my issue, the following prints false:
为了提供有关我的问题的更多详细信息,以下打印错误:
URL url = new URL( "file:///tmp/some%20dir" );
File f = new File( url.getFile() );
System.out.println( "Does dir exist? " + f.exists() );
While the following (manually replacing "%20" with a space) prints true:
虽然以下(用空格手动替换“%20”)打印为真:
URL url = new URL( "file:///tmp/some%20dir" );
File f = new File( url.getFile().replaceAll( "%20", " " ) );
System.out.println( "Does dir exist? " + f.exists() );
Note that I'm not asking whythe first example prints false nor whythe second example using my hacky replaceAllprints true, I'm asking how to convert an escaped URL into a Java File object.
请注意,我没有问为什么第一个例子打印错误,也不为什么用我的哈克第二个例子中的replaceAll打印真实的,我问如何转换逸出的网址为Java File对象。
EDIT: thanks all, this was nearly a dupe but not exactly.
编辑:谢谢大家,这几乎是一个骗局,但不完全是。
Stupidly I was looking for a helper method inside the URL class itself.
愚蠢的是,我正在 URL 类本身中寻找一个辅助方法。
The following works as expected to get a Java File from a Java URL:
以下工作按预期从 Java URL 获取 Java 文件:
URL url = new URL( "file:///home/nonet/some%20dir" );
File f = new File( URLDecoder.decode( url.getFile(), "UTF-8" ) );
采纳答案by Cesar
URLDecoder.decode(url);//deprecated
URLDecoder.decode(url, "UTF-8"); //use this instead
See related question How do you unescape URLs in Java?
请参阅相关问题如何在 Java 中取消转义 URL?
回答by BalusC
The File
constructor taking an URI
in combination with URL#toURI()
should work:
结合 with的File
构造函数URI
URL#toURI()
应该可以工作:
URL url = getItSomehow();
File file = new File(url.toURI());
回答by vorburger
AFAIK like this Paths.get(url.toURI()).toFile()
is best, starting with Java 7. See also Converting Java file:// URL to File(...) path, platform independent, including UNC paths.
Paths.get(url.toURI()).toFile()
从 Java 7 开始,这样的 AFAIK是最好的。另请参阅将 Java file:// URL 转换为 File(...) 路径,与平台无关,包括 UNC 路径。