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();
        }
         
 
    }
}
复制代码

 

posted @   技术小白丁  阅读(280)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示