public class Netaddrutil {
/**
* Parse the hostip<br/> from the URL
* @param URL
* @author Wull
* @return
*/
public static string Getipfromurl (string url) {
1. Determine if it is empty
if (url = = NULL | | Url.trim (). Equals ("")) {
Return "";
}
2. If you are using localhost, replace it with 127.0.0.1
if (Url.startswith ("http//" + C.net.localhost_str)) {
url = url.replace ("http://" + C.NET.LOCALHOST_STR, "http://" + C.net.localhost_num);
}
String host = "";
Pattern p = pattern.compile ("(? <=//|) ((\\w) +\\.) +\\w+ ");
Matcher Matcher = p.matcher (URL);
if (Matcher.find ()) {
Host = Matcher.group ();
}
return host;
}
/**
* Parse the hostip:port<br/> from the URL
* @param URL
* @author Wull * *
public static ipportaddr getipportfromurl (String URL) {
1. Determine if it is empty
if (url = = NULL | | Url.trim (). Equals ("")) {
return null;
}
2. If you are using localhost, replace it with 127.0.0.1
if (Url.startswith ("http//" + C.net.localhost_str)) {
url = url.replace ("http://" + C.NET.LOCALHOST_STR, "http://" + C.net.localhost_num);
}
String host = "";
Pattern p = pattern.compile ("(? <=//|) ((\\w) +\\.) +\\w+ (: \\d{0,5})? ");
Matcher Matcher = p.matcher (URL);
if (Matcher.find ()) {
Host = Matcher.group ();
}
If
if (Host.contains (":") = = False) {
return new IPPORTADDR (host, 80);
}
string[] Ipportarr = Host.split (":");
return new Ipportaddr (Ipportarr[0], Configutil.parseint (ipportarr[1));
}
public static void Main (String [] args) {
String url = "Http://10.33.32.81:8080/login.action";
System.out.println (Netaddrutil.getipfromurl (URL));
ipportaddr addr= netaddrutil.getipportfromurl (URL);
System.out.println (Addr.getip () + "=========>" +addr.getport ());
}
}
Java detaches IP and port from the URL