Java InetAddress类的方法

本文为joshua317原创文章,转载请注明:转载自joshua317博客 Java InetAddress类的方法 - joshua317的博客

Java InetAddress类的方法

这个类表示互联网协议(IP)地址。下面列出了 InetAddress 类常用的方法:

序号方法描述
1static InetAddress getByAddress(byte[] addr) 在给定原始 IP 地址的情况下,返回 InetAddress 对象。
2static InetAddress getByAddress(String host, byte[] addr) 根据提供的主机名和 IP 地址创建 InetAddress。
3static InetAddress getByName(String host) 在给定主机名的情况下确定主机的 IP 地址。
4String getHostAddress() 返回 IP 地址字符串(以文本表现形式)。
5String getHostName() 获取此 IP 地址的主机名。
6static InetAddress getLocalHost() 返回本地主机。
7String toString() 将此 IP 地址转换为 String。
package com.joshua317;

import java.net.InetAddress;
import java.net.UnknownHostException;

public class Main {
    public static void main(String[] args){
        InetAddressTest inetAddress = new InetAddressTest();
        try {
            //根据域名获取
            InetAddress address1 = inetAddress.getInetAddress("www.baidu.com");
            System.out.println(address1);
            System.out.println(address1.getHostName() + "--" +address1.getHostAddress());

            //根据ip获取
            InetAddress address2 = inetAddress.getInetAddressByIp("103.235.46.39");
            System.out.println(address2);
            System.out.println(address2.getHostName() + "--" +address2.getHostAddress());

            //根据byte类型获取
            byte[] bytes = {103,34,46,39};
            InetAddress address3 = inetAddress.getInetAddress(bytes);
            System.out.println(address3);

            //如果数值超出Byte最大范围,需要进行转换,例如:(byte)(235 &0xff)
            System.out.println("Byte最小值:" + Byte.MIN_VALUE);
            System.out.println("Byte最大值:" + Byte.MAX_VALUE);
            byte[] bytes2 = {103,(byte)(235 &0xff),46,39};
            InetAddress address4 = inetAddress.getInetAddress(bytes2);
            System.out.println(address4);

            //查看是否是回环地址
            InetAddress address5 = inetAddress.getInetAddressByIp("127.0.0.1");
            System.out.println(address5.isLoopbackAddress());
            //查看是否是本地地址
            System.out.println(address5.isLinkLocalAddress());

        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }
}
@SuppressWarnings("all")
class InetAddressTest {

    public InetAddressTest() {
    }

    public InetAddress getInetAddress(String host) throws UnknownHostException{
        return InetAddress.getByName(host);
    }
    public InetAddress getInetAddress(byte[] host) throws UnknownHostException{
        return InetAddress.getByAddress(host);
    }

    public InetAddress getInetAddress(String host,byte[] ipByte) throws UnknownHostException{
        return InetAddress.getByAddress(host, ipByte);
    }
    public InetAddress getInetAddressByIp(String ip) throws UnknownHostException{
        String[] ipStr = ip.split("\\.");
        byte[] ipByte = new byte[4];
        for (int i =0; i<ipByte.length; i++) {
            ipByte[i] = (byte) (Integer.parseInt(ipStr[i]) &0xff);
        }
        return InetAddress.getByAddress(ipByte);
    }
}

本文为joshua317原创文章,转载请注明:转载自joshua317博客 Java InetAddress类的方法 - joshua317的博客

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值