手机版
你好,游客 登录 注册
背景:
阅读新闻

Java网络编程之InetAddress和URL

[日期:2015-04-21] 来源:Linux社区  作者:yzl-i [字体:大 中 小]

在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.linuxidc.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();
        }
    }

}

二.URL与URLConnection

1.URL

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

URL类常用方法:

类型 方法 描述
构造方法

URL(String spec)

根据 String 表示形式创建 URL 对象。
构造方法 URL(String protocol, String host, int port, String file) 根据指定 protocol、host、port 号和 file 创建 URL 对象。
URLConnection openConnection() 返回一个 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.linuxidc.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();
        }
    }
}

结果:

显示出来的是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.linuxidc.com");
            //建立连接
            URLConnection urlConn=url.openConnection();
            System.out.println("内容大小:"+urlConn.getContentLength());
            System.out.println("内容类型:"+urlConn.getContentType());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

三.URLEncoder与URLDecoder

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

URLEncoder类的方法:

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

URLDecoder类的方法:

类型 方法 描述
static String decode(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();
        }
       
    }

}

运行结果:

本文永久更新链接地址:http://www.linuxidc.com/Linux/2015-04/116505.htm

linux
相关资讯       Java网络编程 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款