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 g@O.kP 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;
    }

}
相关推荐
憨子周33 分钟前
2M的带宽怎么怎么设置tcp滑动窗口以及连接池
java·网络·网络协议·tcp/ip
霖雨2 小时前
使用Visual Studio Code 快速新建Net项目
java·ide·windows·vscode·编辑器
SRY122404192 小时前
javaSE面试题
java·开发语言·面试
Fiercezm3 小时前
JUC学习
java
无尽的大道3 小时前
Java 泛型详解:参数化类型的强大之处
java·开发语言
ZIM学编程3 小时前
Java基础Day-Sixteen
java·开发语言·windows
我不是星海3 小时前
1.集合体系补充(1)
java·数据结构
P.H. Infinity3 小时前
【RabbitMQ】07-业务幂等处理
java·rabbitmq·java-rabbitmq
爱吃土豆的程序员3 小时前
java XMLStreamConstants.CDATA 无法识别 <![CDATA[]]>
xml·java·cdata
2401_857610034 小时前
多维视角下的知识管理:Spring Boot应用
java·spring boot·后端