替換url中的域名
阿新 • • 發佈:2021-02-14
/**
*
* @param domain 域名
* @param port 埠號
* @param url url路徑
* @return
*/
public static String replaceDomainAndPort(String domain, String port, String url) {
String url_bak = "";
if (url.indexOf("//") != -1) {
String[] splitTemp = url.split("//");
url_bak = splitTemp[0] + "//";
if (port != null) {
url_bak = url_bak + domain + ":" + port;
} else {
url_bak = url_bak + domain;
}
if (splitTemp.length >= 1 && splitTemp[1].indexOf("/") != -1) {
String[] urlTemp2 = splitTemp[1].split("/");
if (urlTemp2.length > 1) {
for (int i = 1; i < urlTemp2.length; i++) {
url_bak = url_bak + "/" + urlTemp2[i];
}
}
System.out.println("url_bak:" + url_bak);
} else {
System.out.println("url_bak:" + url_bak);
}
}
return url_bak;
}
public static void main(String[] args) {
String str = "http://new.dongying.gov.cn/art/2018/8/31/art_43576_3069835.html";
replaceDomainAndPort("www.baidu.com", "8080", str);
}