Java网址url工具类

功能描述

  • 无需引入三方依赖
  • 文本匹配网址(支持多个)
  • 网址解析(包括协议、主机、路径、参数等)
java 复制代码
package com.qiangesoft.image.utils;

import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * url工具类
 *
 * @author qiangesoft
 * @date 2024-04-29
 */
public class UrlHelper {

    /**
     * 正则表达式
     */
    private static final Pattern PATTERN = Pattern.compile("((http|https|ftp):\\/\\/)?([\\w\\-]+\\.)+[\\w\\-]+(:[0-9]+)?(/[\\w\\[\\]\\-.~!*'();:@&=+$,/?#%]*)");

    public static void main(String[] args) throws MalformedURLException {
        String content = "8.97 复制打开抖音,看看【三融在东帝汶的作品】找个没人的地方. 然后一直躺到下午  https://v.douyin.com:8087/i2eTcJ82/?id=111&name=fsfe [email protected] NwS:/ 06/18 ";
        List<String> list = UrlHelper.findAll(content);
        for (String s : list) {
            System.out.println(s);
        }

        String one = UrlHelper.findOne(content);
        System.out.println(UrlHelper.getProtocol(one));
        System.out.println(UrlHelper.getHost(one));
        System.out.println(UrlHelper.getPort(one));
        System.out.println(UrlHelper.getPath(one));
        System.out.println(UrlHelper.getFile(one));
        System.out.println(UrlHelper.getAuthority(one));
        System.out.println(UrlHelper.getQuery(one));
        System.out.println(UrlHelper.getQueryParam(one));
    }

    /**
     * 匹配第一个网址url
     *
     * @param content
     * @return
     */
    public static String findOne(String content) {
        Assert.notNull(content, "content must be not null !");

        List<String> list = findAll(content);
        return CollectionUtils.isEmpty(list) ? null : list.get(0);
    }

    /**
     * 匹配所有网址url
     *
     * @param content
     * @return
     */
    public static List<String> findAll(String content) {
        Assert.notNull(content, "content must be not null !");

        List<String> list = new ArrayList<>();
        findAll(content, (matcher) -> {
            list.add(matcher.group(0));
        });
        return list;
    }

    private static void findAll(String content, Consumer<Matcher> consumer) {
        if (null != content) {
            Matcher matcher = PATTERN.matcher(content);
            while (matcher.find()) {
                consumer.accept(matcher);
            }
        }
    }

    /**
     * 获取协议
     *
     * @param url
     * @return
     * @throws MalformedURLException
     */
    public static String getProtocol(String url) throws MalformedURLException {
        URL urlO = new URL(url);
        return urlO.getProtocol();
    }

    /**
     * 获取主机和端口号
     *
     * @param url
     * @return
     * @throws MalformedURLException
     */
    public static String getAuthority(String url) throws MalformedURLException {
        URL urlO = new URL(url);
        return urlO.getAuthority();
    }

    /**
     * 获取主机
     *
     * @param url
     * @return
     * @throws MalformedURLException
     */
    public static String getHost(String url) throws MalformedURLException {
        URL urlO = new URL(url);
        return urlO.getHost();
    }

    /**
     * 获取端口
     *
     * @param url
     * @return
     * @throws MalformedURLException
     */
    public static int getPort(String url) throws MalformedURLException {
        URL urlO = new URL(url);
        return urlO.getPort() == -1 ? urlO.getDefaultPort() : urlO.getPort();
    }

    /**
     * 获取文件名
     *
     * @param url
     * @return
     * @throws MalformedURLException
     */
    public static String getFile(String url) throws MalformedURLException {
        URL urlO = new URL(url);
        return urlO.getFile();
    }

    /**
     * 获取路径
     *
     * @param url
     * @return
     * @throws MalformedURLException
     */
    public static String getPath(String url) throws MalformedURLException {
        URL urlO = new URL(url);
        return urlO.getPath();
    }

    /**
     * 获取查询参数
     *
     * @param url
     * @return
     * @throws MalformedURLException
     */
    public static String getQuery(String url) throws MalformedURLException {
        URL urlO = new URL(url);
        return urlO.getQuery();
    }

    /**
     * 获取查询参数
     *
     * @param url
     * @return
     * @throws MalformedURLException
     */
    private static Map<String, String> getQueryParam(String url) {
        String regex = "(\\?|&+)(.+?)=([^&]*)";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(url);
        Map<String, String> paramMap = new HashMap<>();
        while (matcher.find()) {
            String key = matcher.group(2);
            String value = matcher.group(3);
            paramMap.put(key, value);
        }
        return paramMap;
    }

}
相关推荐
张张张31219 分钟前
4.2学习总结 Java:list系列集合
java·学习
KATA~22 分钟前
解决MyBatis-Plus枚举映射错误:No enum constant问题
java·数据库·mybatis
xyliiiiiL38 分钟前
一文总结常见项目排查
java·服务器·数据库
shaoing40 分钟前
MySQL 错误 报错:Table ‘performance_schema.session_variables’ Doesn’t Exist
java·开发语言·数据库
腥臭腐朽的日子熠熠生辉1 小时前
解决maven失效问题(现象:maven中只有jdk的工具包,没有springboot的包)
java·spring boot·maven
ejinxian1 小时前
Spring AI Alibaba 快速开发生成式 Java AI 应用
java·人工智能·spring
杉之2 小时前
SpringBlade 数据库字段的自动填充
java·笔记·学习·spring·tomcat
圈圈编码2 小时前
Spring Task 定时任务
java·前端·spring
俏布斯2 小时前
算法日常记录
java·算法·leetcode
27669582922 小时前
美团民宿 mtgsig 小程序 mtgsig1.2 分析
java·python·小程序·美团·mtgsig·mtgsig1.2·美团民宿