java获取本机IP和主机名

时间:2021-08-11 13:38:34

贴代码

GetIP.java

package com.datongsoft.wg.common.util;

import java.net.InetAddress;
public class GetIP {

	/**
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {
		 InetAddress addr = InetAddress.getLocalHost();   
         String ip=addr.getHostAddress().toString(); //获取本机ip   
         String hostName=addr.getHostName().toString(); //获取本机计算机名称   
         System.out.println(ip);
         System.out.println(hostName);
	}

}