举例 url :
http://127.0.0.1:8080/test/123.jpg
需替换成:
http://10.13.165.126:8081/test/123.jpg
实现:
public class Test {
/**
* 将url路径中的域名、端口,https/http
* @param http
* @param domain
* @param port
* @param 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];
}
}
}
}
return url_bak;
}
public static void main(String[] args) {
String url = Test.replaceDomainAndPort("10.13.165.126","8081","http://127.0.0.1:8080/test/123.jpg");
System.out.println("新路径:" + url);
}
}
输出结果: