1、:80/homepage/search.jsp?Key=JAVA为了方便程序员编程,JDK中提供了URL类,该类的全名是.URL,该类用于使用它的各种方法来对URL对象进行分割、合并等处理,如图2-2所示。图1-2 URL类结构URL有6种构造方法,通常使用了绝对URL路径构造方法,其中的URL参数是一个完整的URL字符串,而且必须要包含传输协议,如:URL raceHtml=new URL();四、常用方法1 InetAddress类主要方法bytegetAddress() 返回该对象的原始IP。staticInetAddressgetAllByName(Stringhost) 获得指定主机
2、域名的所有IP地址。InetAddressgetByAddress(byteaddr) 获得指定IP地址对象。getByAddress(Stringhost, byteaddr) 根据指定主机名和IP地址创建对象。getByName(Stringhost) 根据指定主机名获得对象。StringgetCanonicalHostName() 获得指定域名的法定信息。getHostAddress() 返回当前IP地址字符串。getHostName() 获得当前IP地址的主机名。getLocalHost() 获得本地主机。booleanisLoopbackAddress() 判断IP是否为环回地址。2
3、 URL类主要方法getAuthority() 获得URL的认证部分。ObjectgetContent() 获得URL的内容。getContent(Classclasses) 获得URL的内容intgetDefaultPort()获得URL默认的端口号getFile() 获得URL的文件名getHost()获得该URL的主机名getPath() 获得该URL的路径getPort() 获得该URL的端口getProtocol()获得该URL的协议。getQuery()获得该URL的查询部分getRef()获得该URL的锚部分getUserInfo()获得该URL的用户信息URLConnectio
4、nopenConnection() 返回URL的连接。InputStreamopenStream()打开URL的输入读取流四概念填空1. 所有的公开的IP地址由国际组织( NIC )分配,全球有3个网络信息中心,分别是( InterNIC )、( ENIC )、( APNIC )。2. 在IPv4中,A类IP地址以0开头,B类IP地址以( 128 )开头,C类IP地址以( 192 )开头,D类IP地址用于( 224 ),E类IP地址是( 240 )。3. 在IPv4地址日益枯竭的现状,国际IP管理组织推出了3组内部自由分配的IP地址,分别是A类( 10.0.0.0 ),B类( )和C类( )。
5、4. InetAddress是Internet Address的缩写,为了满足IPv4和IPv6的需求,它有2个子类,分别是( )和( Inet6Address )。5. InetAddress类不能通过构造方法获得对象,只能通过( getByName() )、( getByAddress (ip ) )、( getByAllName() )、( getByAddress(主机名。Ip) )、( )等获得InetAddress类对象。6. URL的作用是( internet中对网络资源进行统一的定位和管理的标识 ),其标准格式为( :/端口/?参数 )。7. 中文简体编码为( GB 2312
6、),繁体中文编码为( BIG-5 )。8. 利用JAVA语言如何获得系统信息(通过调用System类中getProperties()方法来得到当前操作系统的信息 )。9. 在JAVA语言中,采用( String类 )进行指定字符串的转换。五程序填空1. 基础InetAddress程序( import java.inet.*; ) /引用网络类库包import java.util.*;class myJava public static void main(String args) ( try ) /异常处理 InetAddress inet = ( InetAddress ).getByNam
7、e(“abc”); /类名 System.out.println(inet.toString(); catch(Exception e) System.err.println(“获得本地信息失败”_+ e.toString()_); /获得异常信息 2. 扫描指定网段的主机情况import .*; String netString = “ 192.168.1.0 ”; try for(int i=0; i 255 ; i+) InetAddress inet =InetAddress. getByAdress (netString+i); System.err.println(e.getMes
8、sage();六、实验内容1该例程使用InetAddress类,用于获得指定的计算机名称和IP地址,在该程序示例中,需要将各注释程序段一一测试运行。class InetAddressDemo public static void main (String args) throws UnknownHostException /练习一,获得本地主机信息 /* InetAddress localAddress = InetAddress.getLocalHost(); System.out.println(localAddress); */ /练习二,获得指定域名主机的信息 String host
9、= ; InetAddress address = InetAddress.getByName(host); System.out.println(address); /练习三,根据指定域名获得所有信息 /注意获得的所有IP地址 InetAddress addresses = InetAddress.getAllByName(host); for(InetAddress address : addresses) System.out.println(address); /练习四,比较根据localhost和计算机名获得信息的不同localhost /更改localhost为你现在所使用计算机名
10、,查看不同 InetAddress ia = InetAddress.getByName (host); System.out.println (Canonical Host Name = + ia.getCanonicalHostName ();Host Address = + ia.getHostAddress ();Host Name = + ia.getHostName ();Is Loopback Address = + ia.isLoopbackAddress (); /练习五,获得本地主机所有IP地址 Enumeration netInterfaces = null; try n
11、etInterfaces = NetworkInterface.getNetworkInterfaces(); while (netInterfaces.hasMoreElements() NetworkInterface ni = netInterfaces.nextElement(); System.out.println(DisplayName: + ni.getDisplayName();Name: + ni.getName();InetAddress ips = ni.getInetAddresses(); while (ips.hasMoreElements() IP: + ips
12、.nextElement().getHostAddress(); catch (Exception e) e.printStackTrace(); */ /练习六,根据IP地址构造InetAddress /* byte ip = new byte (byte) 202, (byte) 117, (byte)128 , 7; /可以更改数值超过255 InetAddress address1 = InetAddress.getByAddress(ip); InetAddress address2 = InetAddress.getByAddress(, ip); System.out.print
13、ln(address1); System.out.println(address2); 报告内容:将以上程序段分别取消注释和运行,并将运行结果截图使用 InetAddress类获得本校或计算机学院的WWW、FTP、MAIL等服务器信息。2 向Windows系统的hosts文件中注册一个域名和IP解析,再使用getByName()方法查看结果。一找到:C:WINDOWSsystem32driversetchosts在该文件中添加一行域名与IP的对应行,例如:192.168.18.100 二再执行3修改hosts文件名为hosts.txt,再执行第二步程序;修改ip地址为888.888.888.8
14、88,再执行第二步程序;在ip地址前添加空格“ ”或其它任意非数字字符。再观察运行结果。将运行结果截图3 该例程采用了URL类,用于解析在单行编辑框中输入的URL地址,分解出访问协议、主机名称、端口号以及文件路径名称等。import .URL;import .MalformedURLException;class myURL URL url; public myURL(String urlStr) try url = new URL(urlStr); catch(Exception e) System.err.println(e.getMessage(); public void resolv
15、e() 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.getQuery(); System.out.println(url.getRef(); myURL my = new myURL(http:/222.24.16.1:8080/home/index
16、.jsp#new?key=java my.resolve(); /图形化的URL输入和分析import java.awt.*;import java.awt.event.*;class getURLInformation extends Frame TextField tUrl = new TextField(输入URL地址 List lItems = new List(20); Button bOk = new Button(解 析 Font f = new Font(Serif,Font.BOLD,30); public getURLInformation() this.setLayout
17、(new BorderLayout(); this.add(tUrl, BorderLayout.NORTH); this.add(lItems, BorderLayout.CENTER); this.add(bOk, BorderLayout.SOUTH); tUrl.setFont(f); lItems.setFont(f); bOk.setFont(f); addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e) dispose(); System.exit(0); ); bOk.addA
18、ctionListener(new Listener(); public static void main(String args) System.out.println(Starting New. getURLInformation mainFrame = new getURLInformation(); mainFrame.setSize(400, 400); mainFrame.setTitle(解析URL mainFrame.setVisible(true); class Listener implements ActionListener public void actionPerf
19、ormed(ActionEvent e) URL getURL = null; try getURL = new URL(tUrl.getText(); catch(MalformedURLException e1) System.out.println(URL解析错误 + e1); lItems.add(Reference: + getURL.getRef(), 0);File Name: + getURL.getFile(), 0);File Path: + getURL.getPath(), 0);Host Port: + getURL.getPort(), 0);Host Name:
20、+ getURL.getHost(), 0);Protocol : + getURL.getProtocol(), 0); 在输入URL时,分别更改以下内容,分别记录运行结果:1) 缺省或笔误访问协议,例如输入URL: 或 htp:/2) 使各部分间隔符号残缺或误用,例如输入URL:/ 或http/:/index.html3) 修改端口号超过655354 通过URL获得网页上文本和图像资源/获得文本资源import java.io.*;class exp_4_7 public static void main(String args) throws Exception String url = 填入任意网页URL /自行输入URL,例如; InputStream in = (new URL(url).openStream(); /获得指定URL的字节输入流 int c = -1; while(c = in.read() != -1) /按字节的方式输入数据和输出数据 System.out.write(c);/获
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1