首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 綜合教學

Java 從 URL 中提取域名:基礎版與隨機批量版實現

     編輯:Java 綜合教學

核心邏輯

域名位於協議後、第一個路徑分隔符(/)或端口號(:)前。標準 URL 結構:

scheme://[user:pass@]host[:port]/path?query#fragment
                ↑_____________↑
              這裡是域名(含子域名)

版本一:單 URL 手動輸入提取

import java.util.Scanner;
import java.util.regex.*;

public class DomainExtractorV1 {
    private static final Pattern DOMAIN_PATTERN = Pattern.compile(
        "^(?:https?://)?(?:[^/@:]+@)?([^:/]+)"
    );

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("請輸入完整 URL:");
        String url = sc.nextLine().trim();

        Matcher m = DOMAIN_PATTERN.matcher(url);
        if (m.find()) {
            System.out.println("提取的域名:" + m.group(1));
        } else {
            System.out.println("無法解析,請檢查 URL 格式");
        }
    }
}

測試案例

輸入輸出
https://www.example.com/pathwww.example.com
https://user:pass@sub.domain.org:8080/apisub.domain.org
ftp://files.server.netfiles.server.net

版本二:從預設列表隨機抽取並提取

import java.util.*;

public class DomainExtractorV2 {
    private static final String[] URL_POOL = {
        "https://www.google.com/search?q=java",
        "https://github.com/spring-projects/spring-boot",
        "https://docs.oracle.com/javase/8/docs/api/",
        "https://stackoverflow.com/questions/tagged/java",
        "https://www.baidu.com/s?wd=spring"
    };

    public static void main(String[] args) {
        Random rand = new Random();
        String url = URL_POOL[rand.nextInt(URL_POOL.length)];
        System.out.println("隨機選取 URL:" + url);

        // 復用 V1 的正則邏輯
        Pattern p = Pattern.compile("^(?:https?://)?(?:[^/@:]+@)?([^:/]+)");
        Matcher m = p.matcher(url);
        if (m.find()) {
            System.out.println("域名:" + m.group(1));
        }
    }
}

進階:使用 JDK 內建 java.net.URI 解析(更健壯)

import java.net.URI;
import java.net.URISyntaxException;

public class DomainExtractorURI {
    public static String extractHost(String url) {
        try {
            URI uri = new URI(url);
            String host = uri.getHost();        // 自動去除 user:pass、port
            return host != null ? host : "解析失敗";
        } catch (URISyntaxException e) {
            return "非法 URL:" + e.getMessage();
        }
    }

    public static void main(String[] args) {
        String[] tests = {
            "https://user:pass@api.example.com:8443/v1/resource",
            "https://localhost:8080/app",
            "invalid-url"
        };
        for (String u : tests) {
            System.out.printf("%-55s → %s%n", u, extractHost(u));
        }
    }
}

輸出

https://user:pass@api.example.com:8443/v1/resource → api.example.com
https://localhost:8080/app                       → localhost
invalid-url                                     → 非法 URL:Illegal character in scheme name at index 0

總結對比

方案優點缺點適用場景
手寫正則無依賴、可精確控制提取規則需自行處理邊界情況(IPv6、非標準協議)簡單提取、教學演示
java.net.URI標準庫、自動處理編碼/端口/認證資訊、RFC 3986 合規嚴格模式下異常 URL 會拋棄異常生產環境、需高可靠性
第三方庫(Guava InternetDomainName、Apache HttpClient)支援公共後綴列表(PSL)、可再提取「根域名」如 example.com引入依賴需區分子域名/根域名、安全審計

最佳實踐建議

  1. 生產代碼優先用 java.net.URI 或 java.net.URL 解析後取 getHost()。
  2. 若需「根域名」(去除子域名),引入 Guava InternetDomainName.from(host).topPrivateDomain()。
  3. 輸入來源不可信時,捕獲 URISyntaxException/MalformedURLException 並記錄日誌,避免拋出干擾主流程。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved