Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I decompose a URL into its component parts in Java?

Tags:

java

My requirements are fairly simple, but I need to do a lot of this so I'm looking for a robust solution.

Is there a good light-weight library for decomposing URLs into their component parts in Java? I'm referring to hostname, query string, etc.

like image 764
sanity Avatar asked Jul 31 '11 16:07

sanity


1 Answers

I am always forgetting the URI format, so here it is:

<scheme>://<userinfo>@<host>:<port><path>#<fragement>

And here an example:

URI uri = new URI ("query://[email protected]:9000/public/manuals/appliances?stove#ge");

The following will happen:

  • uri.getAuthority() will return "[email protected]:9000"
  • uri.getFragment () will return "ge"
  • uri.getHost () will return "books.com"
  • uri.getPath () will return "/public/manuals/appliances"
  • uri.getPort () will return 9000
  • uri.getQuery () will return "stove"
  • uri.getScheme () will return "query"
  • uri.getSchemeSpecificPart () will return "//[email protected]:9000/public/manuals/appliances?stove"
  • uri.getUserInfo () will return "jeff"
  • uri.isAbsolute () will return true
  • uri.isOpaque () will return false

I found this blog handy: Exploring Java's Network API: URIs and URLs

like image 105
Iain Avatar answered Sep 20 '22 02:09

Iain