Java 网络编程

InetAddress类

获取本机的IP地址
InetAddress提供的静态方法getLocalHost()可以获取本机的InetAddress对象。

import java.net.InetAddress;
import java.net.UnknownHostException;

public class MyServerDemo {
    public static void main(String[] args){
        InetAddress localIP=null;
        try{
            localIP=InetAddress.getLocalHost();
        }catch(UnknownHostException e){
            e.printStackTrace();
        }
        System.out.println("LocalHost IP :"+localIP.getHostAddress());
        System.out.println("LocalHost name :"+localIP.getHostName());
    }
}

根据域名获取IP地址
InetAddress提供的InetAddress.getByName(dnsName)方法可以获取指定域名的IP地址。

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Scanner;

public class MyServerDemo {
    public static void main(String[] args){
        Scanner scan=new Scanner(System.in);
        System.out.println("Place intput DNS :");
        String dnsName =scan.next();
        InetAddress dnsIP=null;
        try{
            dnsIP=InetAddress.getByName(dnsName);
        }catch(UnknownHostException e){
            e.printStackTrace();
        }
        System.out.println("DNS IP :"+dnsIP.getHostAddress());
        System.out.println("DNS name :"+dnsIP.getHostName());
    }
}

输出:

Place intput DNS :
www.baidu.com
DNS IP :115.239.211.112
DNS name :www.baidu.com

URL类和URLConnection类
URLConnection 对象经历两个阶段:首先创建对象,然后建立连接。在创建对象之后,建立连接之前,可指定各种选项(例如,doInput 和 UseCaches)。连接后再进行设置就会发生错误。连接后才能进行的操作(例如 getContentLength),如有必要,将隐式执行连接。 所以 有的方法会隐式的执行connect方法,就向 conn = Url.openConnection(); conn.openInputStream();//隐式执行了connect方法。

import java.net.InetAddress;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.Scanner;
import java.io.InputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class URLDemo {
    public static void main(String[] args){
        Scanner scan=new Scanner(System.in);
        System.out.println("Place intput URL :");
        String URLstr =scan.next();
        URL url=null;
        try{
            url=new URL(URLstr);
        }catch(Exception e){
            e.printStackTrace();
        }
        System.out.println("Host :"+url.getHost());
        System.out.println("Path :"+url.getPath());
        System.out.println("getPort :"+url.getPort());
        System.out.println("getProtocol :"+url.getProtocol());
        System.out.println("getUserInfo :"+url.getUserInfo());
        System.out.println("getQuery :"+url.getQuery());
        System.out.println("Page state :");
        try{
            InputStream in=url.openStream();
            BufferedReader bin=new BufferedReader(new InputStreamReader(in));
            String strLine=null;
            while((strLine=bin.readLine())!=null)
            {
                System.out.println(strLine);
            }
            bin.close();
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}
import java.net.URL;
import java.net.URLConnection;
import java.io.InputStream;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
public class URLDemo {
    public static void main(String[] args){
        URL url=null;
        URLConnection urlConn=null;
        try{
            url=new URL("http://fm.baidu.com/");
            urlConn=url.openConnection();
        }catch(Exception e){
            e.printStackTrace();
        }
        System.out.println("getContentLength :"+urlConn.getContentLength());//获取文件大小
        System.out.println("getContentType :"+urlConn.getContentType());//获取文件类型
        try{
            InputStream in=urlConn.getInputStream();//取得资源
            BufferedInputStream bin=new BufferedInputStream(in);
            FileOutputStream fout=new FileOutputStream("index.html");
            int len=0;
            byte b[]=new byte[1024];
            System.out.println("====下载界面====");
            while((len=bin.read(b,0,1024))!=-1)
            {
                fout.write(b,0,len);
            }
            fout.close();
            bin.close();
            in.close();
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}

你可能感兴趣的:(java)