标签:inetaddres url 实例
/*
* InetAddress类
*/
public class Test07 {
public static void main(String[] args) throws UnknownHostException {
//获取本机的InetAddress实例
System.out.println("*******获取本机的InetAddress实例**********");
InetAddress address1 = InetAddress.getLocalHost();
System.out.println("主机名:"+address1.getHostName());
System.out.println("IP地址:"+address1.getHostAddress());
byte[] bytes=address1.getAddress();
System.out.println("字节数组形式的IP地址:"+Arrays.toString(bytes));//超过127则为负数,加256
System.out.println(address1);
//根据主机名获取指定主机的InetAddress实例
System.out.println("*******根据主机名获取指定主机的InetAddress实例**********");
//InetAddress address2=InetAddress.getByName("HE67B6DJBOID2DA");
InetAddress address2=InetAddress.getByName("5.8.6.45");
System.out.println(address2.getHostName());
System.out.println(address2);
//根据字节数组形式的IP地址来获取InetAddress实例
System.out.println("**********根据字节数组形式的IP地址来获取InetAddress实例***********");
byte[] bytes2={(byte)192,(byte)168,6,8};
InetAddress address3=InetAddress.getByAddress(bytes2);
System.out.println(address3.getHostName());
System.out.println(address3);
}
}
/*
* URL常用方法
*/
public class Test08 {
public static void main(String[] args) throws MalformedURLException {
URL baidu=new URL("http://www.baidu.com");
URL url=new URL(baidu, "/index.html?username=tom&password=123#test");
System.out.println("协议:"+url.getProtocol());
System.out.println("主机:"+url.getHost());
System.out.println("端口:"+url.getPort());
System.out.println("文件路径:"+url.getPath());
System.out.println("文件名:"+url.getFile());
System.out.println("相对路径:"+url.getRef());
System.out.println("查询:"+url.getQuery());
}
}
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
/*
* 使用URL读取网页内容
*/
public class Test01 {
public static void main(String[] args) throws IOException {
test02();
}
//读取网页资源
public static void test01() throws IOException{
URL url = new URL("http://localhost:8080/webTest/");
InputStream is = url.openStream();// 获取URL对象所表示的资源的输入流
BufferedReader br = new BufferedReader(new InputStreamReader(is));// 将字节输入流转换为字符输入流
String str = br.readLine();
while (str != null) {
System.out.println(str);
str = br.readLine();
}
br.close();
is.close();
}
//读取FTP上的文件资源
public static void test02() throws IOException{
URL url=new URL("ftp://wbs14061:abc@172.16.1.11/资料/exe2.txt");
System.out.println("协议:"+url.getProtocol());
System.out.println("主机:"+url.getHost());
System.out.println("端口:"+url.getPort());
System.out.println("文件路径:"+url.getPath());
System.out.println("文件名:"+url.getFile());
System.out.println("相对路径:"+url.getRef());
System.out.println("查询:"+url.getQuery());
InputStream is = url.openStream();// 获取URL对象所表示的资源的输入流
BufferedReader br = new BufferedReader(new InputStreamReader(is));// 将字节输入流转换为字符输入流
String str = br.readLine();
while (str != null) {
System.out.println(str);
str = br.readLine();
}
br.close();
is.close();
}
}
JAVA学习笔记(五十八)- InetAddress类与URL
标签:inetaddres url 实例
原文地址:http://blog.csdn.net/wangzi11322/article/details/44887701