Package Org. wiksys; public class spliturl {/*** @ author wiksys * @ Param ARGs */public static void main (string [] ARGs) {read ("http://www.baidu.com/index.jsp ");} public static void read (string URL) {// string url = "http://www.baidu.com: 8080/index. JSP "; // string url2 =" ftp://baidu.com/pub/index.jsp "; int firsplit = URL. indexof ("//"); string proto = URL. substring (0, firsplit + 2); int websplit = URL. indexof ("/", firsplit + 2); int portindex = URL. indexof (":", firsplit); string weburl = URL. substring (firsplit + 2, websplit); string port = ""; if (portindex> = 0) {weburl = weburl. substring (0, weburl. indexof (":"); Port = URL. substring (portindex + 1, websplit); system. out. println ("port:" + port);} string context = URL. substring (websplit); system. out. println ("Protocol:" + PROTO); system. out. println ("url:" + weburl); system. out. println ("content:" + context );}}
The code is very simple, so you don't need to explain it too much. You can simply split the block into four parts based on the URL: If there is no port, it is blocked.