星空网 > 软件开发 > Java

Java网络编程之InetAddress和URL

Java中提供了专门的网络开发程序包---java.net,java的网络编程提供了两种通信协议:TCP(传输控制协议)和UDP(数据报协议)。

一.IP(Internet Protocol) 与InetAddress类

1.IP简介

互联网上的每一台计算机都有一个唯一表示自己的标识,即IP地址。

IP地址=网络地址+主机地址

2.InetAddress

该类主要表示IP地址,有两个子类:Inet4Address、Inet6Address,前者表示IPV4,后者表示IPV6。

InetAddress类的常用方法有:

类型 方法 描述
static InetAddress getByName(String host) 在给定主机名的情况下确定主机的 IP 地址。
static InetAddress

getLocalHost()

返回本地主机。
String getHostName() 获取此 IP 地址的主机名。
boolean isReachable(int timeout) 测试是否可以达到该地址。
测试InetAddress类:
package org.demo.net;import java.net.InetAddress;/** * 测试InetAddress类 * @author oushine */public class InetAddressDemo {  public static void main(String[] args) {    try {      //声明并得到本地InetAddress对象      InetAddress iAddress1=InetAddress.getLocalHost();      //声明并得到远程InetAddress对象      InetAddress iAddress2=InetAddress.getByName("www.baidu.com");      //获得本地IP地址      System.out.println("本机IP地址为:"+iAddress1.getHostAddress());      //获得远程IP地址      System.out.println("百度的IP地址是:"+iAddress2.getHostAddress());      System.out.println("本机是否可达:"+iAddress1.isReachable(3000));    } catch (Exception e) {      e.printStackTrace();    }  }}

结果:

Java网络编程之InetAddress和URL

 

二.URL与URLConnection

1.URL

URL(Uniform Resource Locator)是统一资源定位符,可以直接使用此类找到互联网上的资源(比如一个网页)。

URL类常用方法:

类型方法描述
构造方法

URL(String spec)

根据 String 表示形式创建 URL 对象。
构造方法URL(String protocol, String host, int port, String file)根据指定 protocolhostport 号和 file 创建 URL 对象。
URLConnectionopenConnection()返回一个 URLConnection 对象,它表示到 URL 所引用的远程对象的连接。
InputStream

openStream()

打开到此 URL 的连接并返回一个用于从该连接读入的 InputStream
使用URL读取内容:
package org.demo.net;import java.io.InputStream;import java.net.MalformedURLException;import java.net.URL;import java.util.Scanner;public class UrlDemo {  public static void main(String[] args) {    URL url;    try {      //指定操作的URL      url = new URL("http","www.baidu.com",80,"/index.html");      //打开输入流,读取URL内容      InputStream inputStream=url.openStream();      Scanner scan=new Scanner(inputStream);      //设置读取分隔符      scan.useDelimiter("\n");      while(scan.hasNext()){        //输出内容        System.out.println(scan.next());      }    } catch (Exception e) {      e.printStackTrace();    }  }}

结果:

Java网络编程之InetAddress和URL

显示出来的是HTML代码。

2.URLConnection

URLConnection是封装访问远程网络资源一般方法的类,通过它可以建立与远程服务器的连接,检查远程资源的一些属性。

常用方法:

类型方法描述
int

getContentLength()

返回 content-length 头字段的值。
String

getContentType()

返回 content-type 头字段的值。
InputStream

getInputStream()

返回从此打开的连接读取的输入流。

URLConnection对象可以通过URL类的openConnection()方法取得。

取得URL的基本信息:
package org.demo.net;import java.net.URL;import java.net.URLConnection;public class URLConnectionDemo {  public static void main(String[] args) {    try {      URL url=new URL("http://www.baidu.com");      //建立连接      URLConnection urlConn=url.openConnection();      System.out.println("内容大小:"+urlConn.getContentLength());      System.out.println("内容类型:"+urlConn.getContentType());    } catch (Exception e) {      e.printStackTrace();    }  }}

运行结果:
Java网络编程之InetAddress和URL
 

三.URLEncoder与URLDecoder

在java中如果需要完成编码和解码操作就要使用URLEncoder和URLDecoder两个类。

URLEncoder类的方法:

类型方法描述
static Stringencode(String s, String enc)使用指定的编码机制将字符串转换为 application/x-www-form-urlencoded 格式。

URLDecoder类的方法:

类型方法描述
static Stringdecode(String s, String enc)使用指定的编码机制对 application/x-www-form-urlencoded 字符串解码。
编码及解码操作:
package org.demo.net;import java.net.URLDecoder;import java.net.URLEncoder;public class CodeDemo {  public static void main(String[] args) {    String keyWord="oushine 阳";    try {      String enCode=URLEncoder.encode(keyWord, "UTF-8");      System.out.println("编码之后:"+enCode);      String deCode=URLDecoder.decode(enCode, "UTF-8");      System.out.println("解码之后:"+deCode);    } catch (Exception e) {      e.printStackTrace();    }      }}

运行结果:

Java网络编程之InetAddress和URL




原标题:Java网络编程之InetAddress和URL

关键词:JAVA

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

卖家如何利用Q4挖掘爆款!:https://www.ikjzd.com/articles/8618
Shopify新功能Fraud Protect,助卖家解决欺诈退款投诉问题:https://www.ikjzd.com/articles/8624
读懂亚马逊FBA新政策,不让你的旺季销量输在物流上:https://www.ikjzd.com/articles/8627
AMS是什么?如何利用AMS让亚马逊营销效果最大化?:https://www.ikjzd.com/articles/863
Vine Program火热,你参加了吗:https://www.ikjzd.com/articles/8632
速卖通想要在双11狂欢爆单,现在就应该积极修炼内功,做好产品优化:https://www.ikjzd.com/articles/8640
亚马逊以图搜图功能再更新,99%运营不知道!:https://www.kjdsnews.com/a/1842135.html
短视频广告剪辑技巧 :https://www.kjdsnews.com/a/1842136.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流