获取本机ip和主机名称其实很简单,如下程序即可:
package com.ninemax.module.storage.response.main;
import java.net.InetAddress;
public class Test {
public static void main(String[] args) throws Exception {
InetAddress address = InetAddress.getLocalHost();
InetAddress inetAddress = InetAddress.getLocalHost();
String ip = address.getHostAddress().toString();
String hostName = address.getHostName();
System.out.println(inetAddress);//主机名和ip
System.out.println("ip:" + ip);//ip地址
System.out.println(hostName);//主机名
}
}