公告
财富商城
积分规则
提问
发文
2019-02-21 08:27发布
放我归山
Have a URL, how to retrieve its path part?
http://www.costo.com/test1/test2
How to get "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.
Therefore I wrote three methods, which all use a different way to extract the path from a given URL.
All three methods are invoked 1000000 times for a given URL.
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; }
If you want to get it from an url of your application something like http://localhost:8080/test1/test2/main.jsp. Use can use
request.getRequestURI() //result will be like test1/test2
use URL.getPath() method of URL class.
URL.getPath()
URL
You can do this:
URL url = new URL("http://www.costo.com/test1/test2"); System.out.println(url.getPath());
You want something like this:
String path = new URL("http://www.costo.com/test1/test2").getPath();
Actually that'll give you /test1/test2. You'll just have to remove the first / to get what you want:
/test1/test2
/
path = path.replaceFirst("/", "");
Now you'll have test1/test2 in path.
test1/test2
path
URL url = new URL("http://www.google.com/in/on"); System.out.println(url.getPath());
Also See
最多设置5个标签!
I had performance doubts using the Java URL class for just extracting the path from an URL and thought that this is an overkill.
Therefore I wrote three methods, which all use a different way to extract the path from a given URL.
All three methods are invoked 1000000 times for a given URL.
The result is:
Code - feel free to optimize it:
If you want to get it from an url of your application something like http://localhost:8080/test1/test2/main.jsp. Use can use
use
URL.getPath()
method ofURL
class.You can do this:
You want something like this:
Actually that'll give you
/test1/test2
. You'll just have to remove the first/
to get what you want:Now you'll have
test1/test2
inpath
.Also See