一般我們很容易獲取Windows系統下的ip信息,比如:
- package com.han;
- import java.net.InetAddress;
- import java.net.UnknownHostException;
-
- /**
- * 程序實現了Windows平台下獲得本機ip地址
- * @author HAN
- *
- */
- public class InetAddressObtainment_Windows {
- String hostname;
- String hostaddress;
- void initialize(){
- try {
- InetAddress ia=InetAddress.getLocalHost(); //獲得本機網絡地址對象
- hostname=ia.getHostName(); //獲得對應主機名
- hostaddress=ia.getHostAddress(); //獲得對應主機地址
- } catch (UnknownHostException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- public static void main(String[] args){
- InetAddressObtainment_Windows o1=new InetAddressObtainment_Windows();
- System.out.println(o1.hostname);
- System.out.println(o1.hostaddress);
- o1.initialize();
- System.out.println(o1.hostname);
- System.out.println(o1.hostaddress);
- }
- }