zoukankan      html  css  js  c++  java
  • java网络编程基础——基本网络支持

    基本网络支持

    java.net包主要为网络编程提供支持。

    1、InetAddress

    InetAddress类代表IP地址,还有两个子类:Inet4Address、Inet6Address。

    package net;
     
    import java.io.IOException;
    import java.net.InetAddress;
     
    public class InetAddressTest {
     
        public static void main(String[] args) throws IOException {
             
            //根据主机获取InetAddress
            InetAddress  ip = InetAddress.getByName("www.ruandb.top");
            //判断IP是否可达
            System.out.println("ruandb是否可达:"+ip.isReachable(1000));//ruandb是否可达:false
            //获取IP字符串
            System.out.println(ip.getHostAddress());//47.98.194.49
            //根据Ip地址获取InetAddress
            InetAddress  local = InetAddress.getByAddress(new byte[] {127,0,0,1});
            //判断IP是否可达
            System.out.println("ruandb是否可达:"+local.isReachable(2000));//ruandb是否可达:true
            //获取InetAddress实例对应的全限定域名
            System.out.println(local.getCanonicalHostName());//127.0.0.1
     
        }
    }

    2、URLDecoder和URLEncoder

    当URL地址里包含非西欧字符的字符串时,系统会将这些非西欧字符转换成特殊字符串,这中特殊字符串称为 application/x-www-form-urlencoded MIME字符串。

    URLDecoder和URLEncoder主要就是用于普通字符串和application/x-www-form-urlencoded MIME字符串的转换。

    package net;
     
    import java.io.UnsupportedEncodingException;
    import java.net.URLDecoder;
    import java.net.URLEncoder;
     
    public class URLDecoderTest {
     
        public static void main(String[] args) throws UnsupportedEncodingException {
     
            // 将application/x-www-form-urlencoded MIME字符串转换成普通字符串
            String keyWord = URLDecoder.decode("%E5%8C%97%E4%BA%AC%E8%B7%AF","UTF-8");
            System.out.println(keyWord);//北京路
     
            String urlStr = URLEncoder.encode("北京路", "UTF-8");
            System.out.println(urlStr);//%E5%8C%97%E4%BA%AC%E8%B7%AF
        }
     
    }

    3、URL和URLConnection

    URL对象代表统一资源定位器,它是指向互联网资源的指针。

    JDK中还提供了一个URI类,代表统一的资源标识符,不能用于定位任何资源,它的唯一作用就是解析。

    package net;
     
    import java.io.IOException;
    import java.io.InputStream;
    import java.net.URL;
    import java.net.URLConnection;
     
    public class URLTest {
     
        public static void main(String[] args) throws IOException {
     
            URL url = new URL("http://10.145.198.143:8081/ords/data_service/eda_src/v1.0/tbAddressTags/count?address=AA");
            System.out.println(url.getFile());// 获取URL资源名:/ords/data_service/eda_src/v1.0/tbAddressTags/count?address=AA
            System.out.println(url.getHost());// 获取URL主机名:10.145.198.143
            System.out.println(url.getPath());// 获取URL路径部分:/ords/data_service/eda_src/v1.0/tbAddressTags/count
            System.out.println(url.getPort());// 获取URL端口号:8081
            System.out.println(url.getProtocol());// 获取URL协议名称:http
            System.out.println(url.getQuery());// 获取URL查询字符串部分:address=AA
             
            //获取url连接
            URLConnection con = url.openConnection(); ;
            try ( InputStream inputStream = con.getInputStream();)//获取资源输入流
            {
                byte[] bbuf = new byte[1024];
                int hasRead = 0;
                while((hasRead =inputStream.read(bbuf)) > 0) {
                    System.out.print(new String(bbuf,0,hasRead));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
             
            System.out.println();
             
            try ( InputStream input = url.openStream();)//打开连接获取资源输入流
            {
                byte[] bbuf = new byte[1024];
                int hasRead = 0;
                while((hasRead =input.read(bbuf)) > 0) {
                    System.out.print(new String(bbuf,0,hasRead));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
             
     
        }
    }
  • 相关阅读:
    NHibernate中session.update()及session.merge()的区别
    子序列 (All in All,UVa 10340)
    古老的密码 (Ancient Cipher,NEERC 2004,LA 3213)
    例题1 勇者斗恶龙 (The Dragon of Loowater,UVa 11292)
    HDU1869 六度分离
    B. T-primes
    PoJ 1595 PrimeCuts
    poj 3518 Prime Gap
    PKU1988磁铁
    求组合数
  • 原文地址:https://www.cnblogs.com/jnba/p/10636730.html
Copyright © 2011-2022 走看看