How to get the path of a URL
Have a URL, how to retrieve its path part?
http://www.costo.com/te开发者_JAVA技巧st1/test2
How to get "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
. You'll just have to remove the first /
to get what you want:
path = path.replaceFirst("/", "");
Now you'll have test1/test2
in path
.
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.
- 1st method uses the URL.getPath method from the Java URL class.
- 2nd method uses a regex I found in SO (I lost the source link, otherwise I'd give credits to the author right here).
- 3rd method uses a array-split and join for getting the same result.
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;
}
URL url = new URL("http://www.google.com/in/on");
System.out.println(url.getPath());
Also See
- Javadoc
use URL.getPath()
method of URL
class.
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. Use can use
request.getRequestURI() //result will be like test1/test2
I recommend to use URI
class because that can handle relative path also. Here is a sample code to achieve the same with URI and 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 e.g. /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
.
精彩评论