InetAdress,URL类的使用

本文详细介绍了InetAddress类和URL类的基本用法及实例演示,包括如何获取IP地址、解析主机名、创建URL对象和读取URL内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


1.InetAdress的使用

此类表示互联网协议 (IP) 地址。

InetAddress对象需要通过它提供的静态方法来获取

static InetAddress[] getAllByName(String host)
在给定主机名的情况下,根据系统上配置的名称服务返回其 IP 地址所组成的数组。

static InetAddress getByAddress(byte[] addr)
在给定原始 IP 地址的情况下,返回 InetAddress 对象。

static InetAddress getByAddress(String host,byte[] addr)
根据提供的主机名和 IP 地址创建 InetAddress。

static InetAddress getByName(String host)
在给定主机名的情况下确定主机的 IP 地址。

static InetAddress getLocalHost()
返回本地主机。

import java.io.UnsupportedEncodingException;
import java.net.InetAddress;
import java.net.UnknownHostException;

public class InetAddressTest {

    public static void main(String[] args) throws UnknownHostException, UnsupportedEncodingException {

        // 以www.baidu.com 为例
        String host = "www.baidu.com";
        byte[] addr = new byte[] {};

        InetAddress[] mAddresses;
        InetAddress mAddress;

        // InetAddress[] getAllByName(String host)在给定主机名的情况下,根据系统上配置的名称服务返回其 IP 地址所组成的数组。
        mAddresses = InetAddress.getAllByName(host);
        for (InetAddress inetAddress : mAddresses) {
            System.out.println(inetAddress);
        }

        // InetAddress getByName(String host)在给定主机名的情况下确定主机的 IP 地址。
        mAddress = InetAddress.getByName(host);
        System.out.println(mAddress);
        printInfo(mAddress);

        // 返回此 InetAddress 对象的原始 IP 地址。
        addr = mAddress.getAddress();

        // InetAddress getByAddress(byte[] addr)在给定原始 IP 地址的情况下,返回 InetAddress 对象。
        mAddress = InetAddress.getByAddress(addr);
        printInfo(mAddress);

        // InetAddress getByAddress(String host, byte[] addr) 根据提供的主机名和 IP 地址创建 InetAddress。
        mAddress = InetAddress.getByAddress(host, addr);
        printInfo(mAddress);

        // InetAddress getLocalHost() 返回本地主机。
        mAddress = InetAddress.getLocalHost();
        printInfo(mAddress);

    }
    public static void printInfo(InetAddress inetAddress) {
        System.out.println("完全限定名:" + inetAddress.getCanonicalHostName());
        System.out.println("IP 地址字符串:" + inetAddress.getHostAddress());
        System.out.println("IP 地址的主机名:" + inetAddress.getHostName());
        System.out.println();
    }
}
控制台输出
www.baidu.com/220.181.112.244
www.baidu.com/220.181.111.188
www.baidu.com/220.181.112.244
完全限定名:220.181.112.244
IP 地址字符串:220.181.112.244
IP 地址的主机名:www.baidu.com

完全限定名:220.181.112.244
IP 地址字符串:220.181.112.244
IP 地址的主机名:220.181.112.244

完全限定名:220.181.112.244
IP 地址字符串:220.181.112.244
IP 地址的主机名:www.baidu.com

完全限定名:LaiYaXing
IP 地址字符串:192.168.2.101
IP 地址的主机名:LaiYaXing

2.URL的使用

类 URL 代表一个统一资源定位符,它是指向互联网“资源”的指针。资源可以是简单的文件或目录,也可以是对更为复杂的对象的引用,例如对数据库或搜索引擎的查询。

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

public class URLTest {
    static URL url;
    public static void main(String[] args) throws IOException {
        /*
        查看URL的源码
        public URL(String spec) throws MalformedURLException {
            this(null, spec);
        }
        public URL(URL context, String spec) throws MalformedURLException {
            this(context, spec, null);
        }
        public URL(URL context, String spec, URLStreamHandler handler)
        查看源码得知最终URL(String spec)会调用URL(URL context, String spec, URLStreamHandler handler)
        此时此时context和handler为null。
        在构造函数URL(URL context, String spec, URLStreamHandler handler)中
        如果handler为空会调用 handler = getURLStreamHandler(protocol)的方法根据protocol协议初始化handler
        handler为URLStreamHandler的子类实例
        */


        //构造方法URL(String spec) 
        url = new URL("http://www.baidu.com");
        printUrlInfo(url);



        /*
        URLConnection openConnection()返回一个 URLConnection 对象,它表示到 URL 所引用的远程对象的连接。
        public URLConnection openConnection() throws java.io.IOException {
            return handler.openConnection(this);
        }
         查看源码可以得知openConnection()返回了handler.openConnection(this);
         在构造方法中URL(URL context, String spec, URLStreamHandler handler)已经根据protocol协议自动初始化了handler = getURLStreamHandler(protocol)
         handler.openConnection(this);返回的是URLConnection子类的实例。所以最好把URLConnection转化
         */


        //HttpURLConnection conn=(HttpURLConnection) url.openConnection();
        URLConnection conn=url.openConnection();


        /*
        InputStream openStream() 打开到此 URL 的连接并返回一个用于从该连接读入的 InputStream。
        public final InputStream openStream() throws java.io.IOException {
            return openConnection().getInputStream();
        }
        查看源码可以得知openStream()是先调用了openConnection方法得到一个URLConnection然后再得到URLConnection实例的输入流
        */


        InputStream is = url.openStream();


        //获取输入流内容
        InputStreamReader isr=new InputStreamReader(is);
        BufferedReader br=new BufferedReader(isr);
        String result="";
        String Line = null;
        while ((Line=br.readLine())!=null) {
            result=result+Line;
        }

        System.out.println(result);

    }

    //输出URL信息
    public static void printUrlInfo(URL url) {
        System.out.println("Url:"+url);
        System.out.println("Protocol:"+url.getProtocol());
        System.out.println("Host:"+url.getHost());
        System.out.println("Port"+url.getPort());
        System.out.println("Path:"+url.getPath());
        System.out.println("File:"+url.getFile());
        System.out.println();
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值