獲取本機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);//主機名
}
}