package com.test;
public class SplitUrl {
public static void main(String[] args) { read("http://localhost:8888/esb/server"); } /** * 拆分URL * url */ 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); } String context=url.substring(webSplit); System.out.println("协议:"+proto); System.out.println("网址:"+webUrl); System.out.println("内容:"+context); } }