One of my product requirements is to merge a main URL with the URL in the current url to automatically retrieve the place where the next click should jump. Next I will release the instance code.
For example, a webpage is called
Http://xx.com/a2/
Parse the content of this webpage, which contains many
<A href = "/b2/"> b2 </a>
<A href = "2.html"> 2.html </a>
<A href = "../3.html"> 3.html </a>
If the calculation is performed one by one, it seems to be tolerable. java is used to determine that, for example, "/" at the beginning indicates that the root directory should be added, and "../" indicates that the root directory should be jumped up to a certain level.
What if.../appears. Jump up to Level 2
.../Jump up to 3 levels
There are some unexpected ones, such ./
There is no need to make judgments one by one. java already has built-in functions to solve the url splicing problem.
The Code is as follows: |
Copy code |
JoinUrl ("http://xx.com/a2/", "/b2 /"); |
Everything is done.
The Code is as follows: |
Copy code |
Public static String joinUrl (String curl, String file ){ URL url = null; String q = ""; Try { Url = new URL (curl), file ); Q = url. toExternalForm (); } Catch (MalformedURLException e ){ } Url = null; If (q. indexOf ("#")! =-1) q = q. replaceAll ("^ (. + ?) #.*? $ "," $1 "); Return q; }
|
Used to quickly obtain domain names
The Code is as follows: |
Copy code |
Public static String getDomain (String curl ){ URL url = null; String q = ""; Try { Url = new URL (curl ); Q = url. getHost (); } Catch (MalformedURLException e ){ } Url = null; Return q; } |