用Java自定义一个URL截取参数的工具类

一、主要目的

开发一个工具类,获取URL地址的端口、主机等信息。


二、主要代码如下

(一)定义工具类

/**
 * @Date 2024/3/5
 **/
public class UrlUtil {

    /**
     * 获得协议名
     * @return
     */
    public static String getProtocol(String url){
        if (url != null) {
            return url.substring(0, url.indexOf(":"));
        }
        return "";
    }

    /**
     * 获得服务器地址
     * @param url
     * @return
     */
    public static String getHost(String url){
        // 第一个左斜符的位置
        int pos1 = url.indexOf("/");
        String host1 = url.substring(pos1 + 2);

        int pos2 = host1.indexOf(":");
        if (pos2 != -1){
            return host1.substring(0, pos2);
        } else {
            int pos3 = host1.indexOf("/");
            if (pos3 != -1) {
                return host1.substring(0, pos3);
            } else {
                return host1.substring(0);
            }
        }
    }

    /**
     * 获得端口号
     * @return 端口号
     */
    public static int getPort(String url){
        // 第一个左斜符的位置
        int pos1 = url.indexOf("/");

        // 第一个符号:的位置
        int pos2 = url.indexOf(":");

        // 判断是否有第二个:
        String host = url.substring(pos2 + 3);
        int pos3 = host.indexOf(":");
        if (pos3 != -1){
            // 判断后面有没有路径
            int pos4 = host.indexOf("/");
            if (pos4 != -1){
                return Integer.parseInt(host.substring(pos3 + 1, pos4));
            } else {
                return Integer.parseInt(host.substring(pos3 + 1));
            }
        } else {
            // 返回默认的端口号
            String protocol = getProtocol(url);
            if (protocol.equals("http")){
                return 80;
            } else if(protocol.equals("https")) {
                return 443;
            } else if (protocol.equals("ftp")) {
                return 21;
            }
        }
        return 0;
    }

    /**
     * 获得路径
     * @return
     */
    public static String getPath(String url){

        // 第一次/的位置
        int pos1 = url.indexOf("/");

        // 截取
        String path = url.substring(pos1 + 2);

        // 判断/的位置
        int pos2 = path.indexOf("/");

        if (pos2 == -1){
            return "/";
        }

        // 判断是否有请求参数
        int pos3 = path.indexOf("?");
        if (pos3 != -1) {
            return path.substring(pos2, pos3);
        } else {
            // 判断有没有锚点
            int pos4 = path.indexOf("#");
            if (pos4 != -1){
                return path.substring(pos2, pos4);
            } else {
                return path.substring(pos2);
            }
        }
    }

    /**
     * 获得查询参数
     * @param url
     * @return
     */
    public static String getQueryString(String url){
        // 判断问号的位置
        int pos1 = url.indexOf("?");
        if (pos1 != -1){
            // 判断有没有锚点
            int pos2 = url.indexOf("#");
            if (pos2 != -1){
                return url.substring(pos1 + 1, pos2);
            } else {
                return url.substring(pos1 + 1);
            }
        } else {
            // 返回为空
            return "";
        }
    }

    /**
     * 获得锚点
     * @return
     */
    public static String getFragment(String url){
        // 判断#的位置
        int pos1 = url.indexOf("#");
        // 如果找到了符号'#'
        if (pos1 != -1){
            return url.substring(pos1 + 1);
        } else {
            // 返回为空
            return "";
        }
    }
}


(二)定义测试类

public class URLTest {

    public static void main(String[] args) {
        String url = "http://localhost:8080/admin?name=value#pp";

        System.out.println("协议:" + UrlUtil.getProtocol(url));
        System.out.println("主机地址:" + UrlUtil.getHost(url));
        System.out.println("端口号: " + UrlUtil.getPort(url));
        System.out.println("访问路径: " + UrlUtil.getPath(url));
        System.out.println("查询参数: " + UrlUtil.getQueryString(url));
        System.out.println("锚点: " + UrlUtil.getFragment(url));

    }
}


三、测试类的运行结果如下

在这里插入图片描述


四、总结

1、对于规范的URL地址基本满足使用要求;

2、缺点是没有定义一个方法来判断URL路径是否规范,防止获取信息出现异常;

  • 5
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值