import java.net.InetAddress; public class GetLocalHost { public static void main(String[] args) { InetAddress address = null; try { address = InetAddress.getLocalHost(); } catch (Exception e) { e.printStackTrace(); } System.out.println(address); } }
(2)java根据域名获得IP地址
import java.net.InetAddress; import java.net.UnknownHostException; public class GetIP { public static void main(String[] args) { InetAddress cumt = null; try { cumt = InetAddress.getByName("www.baidu.com"); } catch (UnknownHostException e) { e.printStackTrace(); } System.out.println(cumt); } }
可以看出,与DNS服务器连接只需要一条语句
(3)URL类获取其特征的主要方法:
URL类提供的方法主要包括URL类对象特征(如协议名、主机名、文件名、端口号和标记)的查询和对URL类对象的操纵
String getProtocol();返回URL的协议名
String getHost(); 返回URL的主机名
Int getPort(); 返回URL的端口号,如果没有端口号就返回-1
String getFile(); 返回URL的标记
(4)用URL获取网络上资源的HTML文件
import java.io.DataInputStream; import java.net.URL; public class ReadURL { public static void main(String[] args) { try { //根据参数args[0])构造一个绝对的URL对象 URL url = new URL("http://www.baidu.com/"); //通过URL对象打开一个数据输入流 DataInputStream dis = new DataInputStream(url.openStream()); String inputLine; while((inputLine = dis.readLine()) != null){ System.out.println(inputLine); } dis.close(); } catch (Exception e) { e.printStackTrace(); } } }
(5)用URL获取文本和图像
用URL可以方便的获取文本和图像。文本数据源可以是网上或者本机上的任何文本文件,如果要用URL来获取图像数据,就不能用openStream()方法,而是要使用方法getImage(URL),这个方法会立即生成一个Image对象,并返回程序对象的引用,但这并不意味着图像的数据已经读到内存中,而是系统与此同时产生一个线程去读取图像文件的数据,因此,就可能存在程序已经执行到getImage()后面的语句部分系统还在读取图像文件数据的情形。
import java.awt.Color; import java.awt.Event; import java.awt.Font; import java.awt.Frame; import java.awt.Graphics; import java.awt.Image; import java.awt.Menu; import java.awt.MenuBar; import java.awt.MenuItem; import java.awt.Toolkit; import java.io.DataInputStream; import java.net.URL; //GetDataByURL类继承Frame类,生成一个带标题的窗口 public class GetDataByURL extends Frame{ private static final long serialVersionUID = 1L; private MenuBar menuBar; private boolean drawImage = false; private DataInputStream dataInputStream; private int i = 0; private String line_str; private boolean first = true; private Font font; public GetDataByURL() { //生成一个菜单条 menuBar = new MenuBar(); setMenuBar(menuBar); //为菜单条第一个菜单取名“display” Menu display = new Menu("display"); menuBar.add(display); //生成display菜单下的两个菜单项 MenuItem beanty_display = new MenuItem("display beauty"); MenuItem text_display = new MenuItem("display text"); display.add(beanty_display); display.add(text_display); //设置背景颜色和文本的字体 setBackground(Color.white); font = new Font("System",Font.BOLD,20); //设置带有菜单的窗口的标题 setTitle("sample:use URL get data"); setSize(400,300); //显示窗口 show(); } //处理窗口中的菜单事件 @Override public boolean action(Event evt, Object what) { if(evt.target instanceof MenuItem){ String message = (String)what; if(message == "display beauty"){ drawImage = true; doDrawImage(); }else { drawImage = false; first = true; if(message == "display text"){ doWrite("d:/tt.txt"); } } } return true; } //处理窗事件 @Override public boolean handleEvent(Event evt) { switch(evt.id){ case Event.WINDOW_DESTROY:{ dispose(); System.exit(0); } default:{ return super.handleEvent(evt); } } } public static void main(String[] args) { new GetDataByURL(); } public void paint(Graphics g){ if(drawImage){ try { URL image_url = new URL("https://www.ibm.com/developerworks/cn/web/wa-lo-json/fig001.jpg"); Toolkit object_Toolkit = Toolkit.getDefaultToolkit(); Image object_Image = object_Toolkit.getImage(image_url); g.setColor(Color.white); g.fillRect(0, 0, 300, 400); g.drawImage(object_Image, 40, 80, 160,200,this); } catch (Exception e) { e.printStackTrace(); } }else { if(first){ first = false; g.setColor(Color.white); g.fillRect(0, 0, 400, 300); g.setFont(font); } if(line_str != null){ g.drawString(line_str, 10, i*20); i++; } } } private void doDrawImage(){ drawImage = true; repaint(); } private void doWrite(String url_str){ try { //用参数url_str生成一个绝对的URL,它指向本机上的一个文本文件 URL url = new URL(url_str); dataInputStream = new DataInputStream(url.openStream()); try { i = 1; line_str = dataInputStream.readLine(); while(line_str != null){ paint(getGraphics()); line_str = dataInputStream.readLine(); } } catch (Exception e) { e.printStackTrace(); } } catch (Exception e) { e.printStackTrace(); } } }
(6)URLConection()类
URL类仅提供读取地址为URL的web服务器内容的方法,如果除了读取其内容外,还要向URL对象发送服务请求及参数,那么必须使用URLConnection()类,利用URL类提供的openConnection()方法可以建立一个URLConnection类对象。然后,可以使用这个URLConection类对象绑定的数据输入流读URL的内容,使用这个URLconnection类对象绑定数据输出流发送服务请求及参数
URLConection类是一个以HTTP为中心的类,同时支持get()和post()方法,默认情况下为POST方法。
URL和URLConnection的区别在于前者代表一个资源的位置,后者代表一种连接。
import java.io.DataInputStream; import java.net.URL; import java.net.URLConnection; public class ReadURLConnection { public static void main(String[] args) { try { //根据绝对URL地址构造URL对象 URL cumtURL = new URL("http://www.baidu.com"); //利用cumtURL建立一个URLconnection连接 URLConnection cumtConnection = cumtURL.openConnection(); //获取cumtConnection对象的数据输入流 DataInputStream din = new DataInputStream(cumtConnection.getInputStream()); String inputLine; while((inputLine = din.readLine()) != null){ System.out.println(inputLine); } din.close(); } catch (Exception e) { e.printStackTrace(); } } }