[英]How to get the path of a URL
Have a URL, how to retrieve its path part?有一个URL,如何获取它的路径部分?
http://www.costo.com/test1/test2 http://www.costo.com/test1/test2
How to get "test1/test2"如何获得“test1/test2”
You want something like this: 你想要这样的东西:
String path = new URL("http://www.costo.com/test1/test2").getPath();
Actually that'll give you /test1/test2
. 实际上,这将给你
/test1/test2
。 You'll just have to remove the first /
to get what you want: 你只需删除第一个
/
获得你想要的东西:
path = path.replaceFirst("/", "");
Now you'll have test1/test2
in path
. 现在你将在
path
有test1/test2
。
I had performance doubts using the Java URL class for just extracting the path from an URL and thought that this is an overkill. 我对使用Java URL类进行性能疑问只是从URL中提取路径并认为这是一种过度杀伤力。
Therefore I wrote three methods, which all use a different way to extract the path from a given URL. 因此,我写了三个方法,它们都使用不同的方法从给定的URL中提取路径。
All three methods are invoked 1000000 times for a given URL. 对于给定的URL,所有三种方法都被调用1000000次。
The result is: 结果是:
#1 (getPathviaURL) took: 860ms
#2 (getPathViaRegex) took: 3763ms
#3 (getPathViaSplit) took: 1365ms
Code - feel free to optimize it: 代码 - 随意优化它:
public static void main(String[] args) {
String host = "http://stackoverflow.com/questions/5564998/how-to-get-the-path-of-a-url";
long start1 = System.currentTimeMillis();
int i = 0;
while (i < 1000000) {
getPathviaURL(host);
i++;
}
long end1 = System.currentTimeMillis();
System.out.println("#1 (getPathviaURL) took: " + (end1 - start1) + "ms");
Pattern p = Pattern.compile("(?:([^:\\/?#]+):)?(?:\\/\\/([^\\/?#]*))?([^?#]*)(?:\\?([^#]*))?(?:#(.*))?");
long start2 = System.currentTimeMillis();
int i2 = 0;
while (i2 < 1000000) {
getPathViaRegex(host, p);
i2++;
}
long end2 = System.currentTimeMillis();
System.out.println("#2 (getPathViaRegex) Took: " + (end2 - start2) + "ms");
long start3 = System.currentTimeMillis();
int i3 = 0;
while (i3 < 1000000) {
getPathViaSplit(host);
i3++;
}
long end3 = System.currentTimeMillis();
System.out.println("#3 (getPathViaSplit) took: " + (end3 - start3) + "ms");
}
public static String getPathviaURL(String url) {
String path = null;
try {
path = new URL(url).getPath();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return path;
}
public static String getPathViaRegex(String url, Pattern p) {
Matcher m = p.matcher(url);
if (m.find()) {
return m.group(3);
}
return null;
}
public static String getPathViaSplit(String url) {
String[] parts = url.split("/");
parts = Arrays.copyOfRange(parts, 3, parts.length);
String joined = "/" + StringUtils.join(parts, "/");
return joined;
}
使用URL
类的URL.getPath()
方法。
You can do this: 你可以这样做:
URL url = new URL("http://www.costo.com/test1/test2");
System.out.println(url.getPath());
If you want to get it from an url of your application something like http://localhost:8080/test1/test2/main.jsp . 如果你想从你的应用程序的URL获取它,如http:// localhost:8080 / test1 / test2 / main.jsp 。 Use can use
使用可以使用
request.getRequestURI() //result will be like test1/test2
I recommend to use URI
class because that can handle relative path also.我建议使用
URI
class,因为它也可以处理相对路径。 Here is a sample code to achieve the same with URI and URL:这是使用 URI 和 URL 实现相同功能的示例代码:
String urlStr = "http://localhost:8080/collections-in-java?error=true";
try {
URI uri = URI.create(urlStr);
System.out.println(uri.getPath());
URL url1 = new URL(urlStr);
System.out.println(url1.getPath());
} catch (MalformedURLException e) {
e.printStackTrace();
}
The above code will produce same result.上面的代码将产生相同的结果。 The URI is useful if there is chance that the path may be relative eg
/some/path/collections-in-java?error=true
如果路径可能是相对的,则 URI 很有用,例如
/some/path/collections-in-java?error=true
For this case, URI.getPath()
will return /some/path/collections-in-java
but URL.getPath()
will throw MalformedURLException
.对于这种情况,
URI.getPath()
将返回/some/path/collections-in-java
但URL.getPath()
将抛出MalformedURLException
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.