package com.example.datacollect.util; import java.net.URI; import java.net.URISyntaxException; import java.util.regex.Pattern; /** * URL 格式验证工具类 * 用于判断输入的字符串是否是合法的 HTTP/HTTPS URL */ public class UrlValidator { private static final String HTTP_PROTOCOL = "http"; private static final String HTTPS_PROTOCOL = "https"; private static final Pattern DOMAIN_PATTERN = Pattern.compile( "^[a-zA-Z0-9]([a-zA-Z0-9-]*[a-zA-Z0-9])?(\\.[a-zA-Z0-9]([a-zA-Z0-9-]*[a-zA-Z0-9])?)*$" ); /** * 验证 URL 是否合法 * @param url 要验证的 URL 字符串 * @return 是否为合法的 HTTP/HTTPS URL */ public static boolean isValidUrl(String url) { if (url == null || url.trim().isEmpty()) { return false; } try { URI uri = new URI(url); String scheme = uri.getScheme(); if (scheme == null) { return false; } String schemeLower = scheme.toLowerCase(); if (!HTTP_PROTOCOL.equals(schemeLower) && !HTTPS_PROTOCOL.equals(schemeLower)) { return false; } String host = uri.getHost(); if (host == null || host.isEmpty()) { return false; } if (!DOMAIN_PATTERN.matcher(host).matches()) { return false; } return true; } catch (URISyntaxException e) { return false; } } /** * 验证 URL 并返回详细信息 * @param url 要验证的 URL 字符串 * @return 验证结果描述 */ public static String validateWithMessage(String url) { if (url == null || url.trim().isEmpty()) { return "无效:URL 不能为空"; } try { URI uri = new URI(url); String scheme = uri.getScheme(); if (scheme == null) { return "无效:缺少协议 scheme(如 http://)"; } String schemeLower = scheme.toLowerCase(); if (!HTTP_PROTOCOL.equals(schemeLower) && !HTTPS_PROTOCOL.equals(schemeLower)) { return "无效:协议必须是 http 或 https,当前为:" + scheme; } String host = uri.getHost(); if (host == null || host.isEmpty()) { return "无效:缺少主机名"; } return "有效 URL:" + url; } catch (URISyntaxException e) { return "无效:URL 格式错误 - " + e.getMessage(); } } /** * 获取 URL 的协议 * @param url URL 字符串 * @return 协议名称,如果无效返回 null */ public static String getProtocol(String url) { try { URI uri = new URI(url); return uri.getScheme(); } catch (URISyntaxException e) { return null; } } /** * 测试主方法 */ public static void main(String[] args) { String[] testUrls = { "https://www.example.com", "http://localhost:8080/api", "https://github.com/user/repo", "ftp://ftp.example.com/file", "htp://invalid.protocol", "not a url", "", "https://192.168.1.1:8080", "https://sub.domain.example.com/path/to/page", "javascript:alert(1)" }; System.out.println("===== URL 格式验证测试 =====\n"); for (String url : testUrls) { String result = validateWithMessage(url); System.out.println("测试: " + (url.isEmpty() ? "(空字符串)" : url)); System.out.println("结果: " + result); System.out.println(); } } }