简体   繁体   中英

Utils to get path from URL

I need to get the path after the domain name.
Ex: The url is https://vaadin.com:8080/wiki/-/wiki/Main/Authenticating+Vaadin-based+applications/pop_up
I want to get /wiki/-/wiki/Main/Authenticating+Vaadin-based+applications/pop_up . Is there any Utils class for java to do this quickly? Thanks so much!

Why don't you just use getPath() of URL class? see here:

http://docs.oracle.com/javase/6/docs/api/java/net/URL.html#getPath%28%29

Class URL represents a Uniform Resource Locator, a pointer to a "resource" on the World Wide Web. A resource can be something as simple as a file or a directory, or it can be a reference to a more complicated object, such as a query to a database or to a search engine. More information on the types of URLs and their formats can be found at:

 http://www.socs.uts.edu.au/MosaicDocs-old/url-primer.html 

In general, a URL can be broken into several parts. The previous example of a URL indicates that the protocol to use is http (HyperText Transfer Protocol) and that the information resides on a host machine named www.socs.uts.edu.au. The information on that host machine is named /MosaicDocs-old/url-primer.html. The exact meaning of this name on the host machine is both protocol dependent and host dependent. The information normally resides in a file, but it could be generated on the fly. This component of the URL is called the path component.

As @Yan Foto suggested the best way is to use java.net.URL class.

Example:

URL url = new URL("https://vaadin.com:8080/wiki/-/wiki/Main/Authenticating+Vaadin-based+applications/pop_up");

System.out.print("Path: " + url.getPath());

It prints following output :

Path: wiki/-/wiki/Main/Authenticating+Vaadin-based+applications/pop_up

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM