加入收藏 | 设为首页 | 会员中心 | 我要投稿 十堰站长网 (https://www.0719zz.com/)- 混合云存储、网络、视频终端、云计算、媒体处理!
当前位置: 首页 > 编程开发 > Java > 正文

java 开发中网络编程之IP、URL详解及实例代码

发布时间:2021-03-30 17:05:57 所属栏目:Java 来源:互联网
导读:java网络编程java.net类InetAddress此类表示互联网协议(IP)地址。会抛出异常UnknownHostException

java 网络编程

java.net

类 InetAddress 此类表示互联网协议 (IP) 地址。 会抛出异常 UnknownHostException

  直接已知子类: 

       Inet4Address,Inet6Address

没有构造函数,但是可以通过静态方法获取对象后,在完成其它功能的使用。

 例如:

  static InetAddress getLocalHost() 返回本地主机。   
  static InetAddress getByName(String host) 在给定主机名的情况下确定主机的 IP 地址。
  static InetAddress[] getAllByName(String host) 在给定主机名的情况下,根据系统上配置的名称服务返回其 IP 地址所组成的数组。         
  String getHostAddress() 返回 IP 地址字符串(以文本表现形式)。  
  String getHostName() 获取此 IP 地址的主机名  
  String getCanonicalHostName()  获取此 IP 地址的完全限定域名。即将主机名解析为IP地址

例子1:

import java.net.*;
class IPDemo
{
  public static void main(String[] args) throws Exception
  {
    // InetAddress localhost = InetAddress.getLocalHost(); 
    // System.out.println("localhost="+localhost);       //返回本地主机(主机名和IP地址)
    
    // String hostname = localhost.getHostName();        //返回本地主机中的主机名
    // String hostIP = localhost.getHostAddress();       //返回本地主机中的IP地址
    // System.out.println("hostname="+hostname+"n"+"hostIP="+hostIP);
    
    
    //InetAddress ia = InetAddress.getByName("www.baidu.com");  
    //System.out.println("name="+ia.getHostName());       
    //System.out.println("adress="+ia.getHostAddress());       


    InetAddress[] iad = InetAddress.getAllByName("www.baidu.com");//百度提供的不止一个主机
    for(int i=0;i<iad.length;i++)
    {
     System.out.println("name="+iad[i].getHostName());       
     System.out.println("adress="+iad[i].getHostAddress());       
    }      
  }
}

import java.net.*;
 String getFile() 获取此 URL 的文件名。 
 String getHost() 获取此 URL 的主机名(如果适用)。 
 String getPath() 获取此 URL 的路径部分。 
 int getPort() 获取此 URL 的端口号。 
 String getProtocol() 获取此 URL 的协议名称。 
 String getQuery() 获取此 URL 的查询部分。

例子2:URL使用

class URLDemo
{
  public static void main(String[] args)throws Exception
  {
    URL url = new URL("http://192.168.1.105:8080/myweb/demo.html?name=haha&age=20");
    
    System.out.println("getProtocol() :"+url.getProtocol());
    System.out.println("getHost() :"+url.getHost());
    System.out.println("getPort() :"+url.getPort());
    System.out.println("getFile() :"+url.getFile());
    System.out.println("getPath() :"+url.getPath());
    System.out.println("getQuery() :"+url.getQuery());
  }
} 

例子3:URLConnection连接

import java.io.*;
import java.net.*;
class URLConnectionDemo
{
  public static void main(String[] args)throws Exception
  {
    URL url = new URL("http://192.168.1.105:8080/myweb/demo.html");
    
    URLConnection conn = url.openConnection(); 
    
    InputStream in = conn.getInputStream();
    
    byte[] buf = new byte[1024];
    int len = in.read(buf);
    
    System.out.println(new String(buf,len));  
  }
}

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

(编辑:十堰站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读