Inetaddress类
Inetaddress类可以用来表示IP地址,该类有两个子类Inet4Address和Inet6Address,分别表示IPv4和IPv6。
注意:
Inetaddress类并没有提供构造方法,但可以通过类中的以下静态方法获取Inetaddress类的实例:
getAllByName() | getByAddress() |
---|---|
getLocalHost() | getByName() |
Inetaddress类中的常用方法:
方法 | 作用 |
---|---|
String getHostAddress() | 返回IP地址字符串 |
String getHostName() | 返回此IP地址的主机名 |
URL类和URLConnection类
URL类
URL类代表一个统一的资源定位符,它是指向互联网“资源”的指针;资源可以是简单的文件或目录,也可以是复杂的对象引用;每一个URL对象都分装了资源标识符和协议处理程序。获得URL对象的方式有多种,一种是调用toURI()方法,一种是直接通过URL的构造方法创建对象。
常用方法:
方法 | 作用 |
---|---|
getFile() | 获取此URL的文件名 |
getHost() | 获取此URL的的主机名 |
getPath() | 获取此URL的路径部分 |
getPort() | 获取此URL的端口号,若未设置,返回-1 |
getProtocol() | 获取此URL的协议名称 |
getQuery() | 获取此URL的查询部分 |
InputStream openStream() | 打开此处的URL的连接,并返回一个用于从该处连接读入的InputStream |
URLConnection openConnection() | 获取URLConnection对象 |
URLConnection类
URLConnection类可以与URL建立动态的连接,同时使用URLConnection对象可以实现向服务器发送请求,将数据送回服务器并检查远程资源的一些属性;
一般创建URLConnection类的对象使用的是URL的 openConnection()方法;
方法 | 作用 |
---|---|
int getContentLength() | 获取远程资源文件大小 |
String getContentType() | 获取远程资源文件类型 |
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Scanner;
/**
* @ClassName TestDemo3
* @Description URL
* @Author lzq
* @Date 2018/12/8 18:39
* @Version 1.0
**/
public class TestDemo3 {
public static void main(String[] args) {
System.out.println("请输入URL:"); //http://www.baidu.com
Scanner scanner = new Scanner(System.in);
String urlstr = scanner.next();
URL url = null;
try {
url = new URL(urlstr);
}catch (Exception e) {
e.printStackTrace();
}
System.out.println("主机:"+url.getHost());
System.out.println("路径:"+url.getPath());
System.out.println("端口号:"+url.getPort());
System.out.println("协议:"+url.getProtocol());
System.out.println("用户信息:"+url.getUserInfo());
System.out.println("查询组成:"+url.getQuery());
System.out.println("网页内容:");
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.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
/**
* @ClassName TestDemo4
* @Description URLConnection类
* @Author lzq
* @Date 2018/12/8 18:51
* @Version 1.0
**/
public class TestDemo4 {
public static void main(String[] args) {
URL url = null;
URLConnection urlConnection = null;
try {
url = new URL("http://www.baidu.com/index.html");
urlConnection = url.openConnection();
}catch (Exception e) {
e.printStackTrace();
}
System.out.println("文件大小:"+urlConnection.getContentLength());
System.out.println("文件类型:"+urlConnection.getContentType());
try {
InputStream in = urlConnection.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();
System.out.println("===下载结束===");
}catch (Exception e){
e.printStackTrace();
}
}
}