替换url中的域名
/**
*
* @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); }
本文来自博客园,作者:土木转行的人才,转载请注明原文链接