使用java获取计算机的IP和hostname - - ITeye技术网站
- import java.util.*;
- import java.net.*;
- public class TestIP
- {
- public static void main(String[] args)
- {
- String IP = null;
- String host = null;
- try
- {
- InetAddress ia = InetAddress.getLocalHost();
- host = ia.getHostName();//获取计算机主机名
- IP= ia.getHostAddress();//获取计算机IP
- }
- catch(UnknownHostException e)
- {
- e.printStackTrace();
- }
- System.out.println(host);
- System.out.println(IP);
- }
- }