java url.getFile() 和 getpath() 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11928199/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 06:58:22  来源:igfitidea点击:

What's the difference between url.getFile() and getpath()?

javahttpurlpath

提问by Jeff Axelrod

In java.net.urlthere is a getFile()method and a getPath()method.

java.net.url有一种getFile()方法和一种getPath()方法。

In my testing, they both return the same result: the full path and file after the domain name trailing slash.

在我的测试中,它们都返回相同的结果:域名尾部斜杠后的完整路径和文件。

For instance, http://www.google.com/x/y/z.htmlreturns x/y/z.htmlfor both methods.

例如,两种方法都http://www.google.com/x/y/z.html返回x/y/z.html

Could someone elaborate on the Javadocs?

有人可以详细说明Javadocs吗?

回答by Jon Lin

The URL.getFile()javadocs say this:

URL.getFile()javadocs中这样说:

Gets the file name of this URL. The returned file portion will be the same as getPath(), plus the concatenation of the value of getQuery(), if any. If there is no query portion, this method and getPath() will return identical results.

获取此 URL 的文件名。返回的文件部分将与 getPath() 相同,加上 getQuery() 的值的串联(如果有)。如果没有查询部分,此方法和 getPath() 将返回相同的结果。

They will be the same unless there is a query string, e.g. a ?somename=value&somethingelse=value2in the URL.

除非有查询字符串,例如?somename=value&somethingelse=value2URL 中的a ,否则它们将相同。

回答by Mohammod Hossain

URL.getFile():

URL.getFile():

Gets the file name of this URL. The returned file portion will be the same as getPath(), plus the concatenation of the value of getQuery(), if any. If there is no query portion, this method and getPath() will return identical results.

获取此 URL 的文件名。返回的文件部分将与 getPath() 相同,加上 getQuery() 的值的串联(如果有)。如果没有查询部分,此方法和 getPath() 将返回相同的结果。

回答by epox

File, in the general case, is longer

文件,在一般情况下,更长

new URL("http://www.google.com/x/y/z.html?v=1#chapter1").getFile();
// returns:  "/x/y/z.html?v=1"

, than Path:

,比路径:

new URL("http://www.google.com/x/y/z.html?v=1#chapter1").getPath();
// returns:  "/x/y/z.html"