在Java中,可以使用InetAddress.getLocalHost()来获取运行Java应用程序的当前服务器的IP地址。

package com.mkyong;

import java.net.InetAddress;
import java.net.UnknownHostException;

public class Test {

	public static void main(String[] args) {

	  InetAddress ip;
	  try {

		ip = InetAddress.getLocalHost();
		System.out.println("Current IP address : " + ip.getHostAddress());

	  } catch (UnknownHostException e) {

		e.e.printStackTrace();

	  }

	}

}

输出量

Current IP address : 192.168.1.22

参考文献

  1. InetAddress Java文档
  2. 如何在Java中获取客户端IP地址


翻译自: https://mkyong.com/java/how-to-get-ip-address-in-java/