JAVA 截取URL

       在开发的过程中有时我们会面临着截取URL,例如在拦截器或者在鉴权中心需要对url进行处理的时候,下面给大家分享下常见的对url的截取获取到我们所需的部分。

import java.net.URL;  
import java.net.MalformedURLException;  
  
public class Test {  
    public static void main(String[] args) {  
        try {  
            URL url = new URL("http://127.0.0.1:8080/path/to/resource?param=xxxx#xxx");  
  
            // 获取整个URL  
            String fullURL = url.toString();  
            System.out.println("Full URL: " + fullURL);  
  
            // 获取协议部分  
            String protocol = url.getProtocol();  
            System.out.println("Protocol: " + protocol);  
  
            // 获取主机名  
            String host = url.getHost();  
            System.out.println("Host: " + host);  
  
            // 获取端口号(如果没有指定端口,则返回-1)  
            int port = url.getPort();  
            System.out.println("Port: " + por
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

清风&竹林

各位老板的鼓励是我最大的创作动

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值