java获取本机IP和主机名

时间:2022-06-11 13:39:41

贴代码

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);
	}

}