精华内容
下载资源
问答
  • Java中,可以使用InetAddress.getLocalHost()来获取运行Java应用程序的当前服务器IP地址。 package com.mkyong; import java.net.InetAddress; import java.net.UnknownHostException; public class Test { ...

    在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/

    展开全文
  • 这篇文章主要介绍了java编程实现获取机器IP地址及MAC地址的方法,实例分析了Java分别针对单网卡及多网卡的情况下获取服务器IP地址与MAC地址的相关技巧,需要的朋友可以参考下 本文实例讲述了java编程实现...
    这篇文章主要介绍了java编程实现获取机器IP地址及MAC地址的方法,实例分析了Java分别针对单网卡及多网卡的情况下获取服务器IP地址与MAC地址的相关技巧,需要的朋友可以参考下
     

    本文实例讲述了java编程实现获取服务器IP地址及MAC地址的方法。分享给大家供大家参考,具体如下:

    已测系统:
    windows linux unix

    排除127.0.0.1 和 0.0.0.0.1等非正常IP

    import java.net.InetAddress;
    import java.net.NetworkInterface;
    import java.net.SocketException;
    import java.util.ArrayList;
    import java.util.Enumeration;
    import java.util.List;
    public class IpUtil {
    private IpUtil(){}
    /**
    * 此方法描述的是:获得服务器的IP地址
    * @author: zhangyang33@sinopharm.com
    * @version: 2014年9月5日 下午4:57:15
    */
    public static String getLocalIP() {
    String sIP = "";
    InetAddress ip = null;
    try {
    boolean bFindIP = false;
    Enumeration<NetworkInterface> netInterfaces = (Enumeration<NetworkInterface>) NetworkInterface
    .getNetworkInterfaces();
    while (netInterfaces.hasMoreElements()) {
    if (bFindIP) {
    break;
    }
    NetworkInterface ni = (NetworkInterface) netInterfaces
    .nextElement();
    Enumeration<InetAddress> ips = ni.getInetAddresses();
    while (ips.hasMoreElements()) {
    ip = (InetAddress) ips.nextElement();
    if (!ip.isLoopbackAddress()
    && ip.getHostAddress().matches(
    "(\\d{1,3}\\.){3}\\d{1,3}")) {
    bFindIP = true;
    break;
    }
    }
    }
    } catch (Exception e) {
    OutUtil.error(IpUtil.class, e.getMessage());
    }
    if (null != ip) {
    sIP = ip.getHostAddress();
    }
    return sIP;
    }
    /**
    * 此方法描述的是:获得服务器的IP地址(多网卡)
    * @author: zhangyang33@sinopharm.com
    * @version: 2014年9月5日 下午4:57:15
    */
    public static List<String> getLocalIPS() {
    InetAddress ip = null;
    List<String> ipList = new ArrayList<String>();
    try {
    Enumeration<NetworkInterface> netInterfaces = (Enumeration<NetworkInterface>) NetworkInterface
    .getNetworkInterfaces();
    while (netInterfaces.hasMoreElements()) {
    NetworkInterface ni = (NetworkInterface) netInterfaces
    .nextElement();
    Enumeration<InetAddress> ips = ni.getInetAddresses();
    while (ips.hasMoreElements()) {
    ip = (InetAddress) ips.nextElement();
    if (!ip.isLoopbackAddress()
    && ip.getHostAddress().matches(
    "(\\d{1,3}\\.){3}\\d{1,3}")) {
    ipList.add(ip.getHostAddress());
    }
    }
    }
    } catch (Exception e) {
    OutUtil.error(IpUtil.class, e.getMessage());
    }
    return ipList;
    }
    /**
    * 此方法描述的是:获得服务器的MAC地址
    * @author: zhangyang33@sinopharm.com
    * @version: 2014年9月5日 下午1:27:25
    */
    public static String getMacId() {
    String macId = "";
    InetAddress ip = null;
    NetworkInterface ni = null;
    try {
    boolean bFindIP = false;
    Enumeration<NetworkInterface> netInterfaces = (Enumeration<NetworkInterface>) NetworkInterface
    .getNetworkInterfaces();
    while (netInterfaces.hasMoreElements()) {
    if (bFindIP) {
    break;
    }
    ni = (NetworkInterface) netInterfaces
    .nextElement();
    // ----------特定情况,可以考虑用ni.getName判断
    // 遍历所有ip
    Enumeration<InetAddress> ips = ni.getInetAddresses();
    while (ips.hasMoreElements()) {
    ip = (InetAddress) ips.nextElement();
    if (!ip.isLoopbackAddress() // 非127.0.0.1
    && ip.getHostAddress().matches(
    "(\\d{1,3}\\.){3}\\d{1,3}")) {
    bFindIP = true;
    break;
    }
    }
    }
    } catch (Exception e) {
    OutUtil.error(IpUtil.class, e.getMessage());
    }
    if (null != ip) {
    try {
    macId = getMacFromBytes(ni.getHardwareAddress());
    } catch (SocketException e) {
    OutUtil.error(IpUtil.class, e.getMessage());
    }
    }
    return macId;
    }
    /**
    * 此方法描述的是:获得服务器的MAC地址(多网卡)
    * @author: zhangyang33@sinopharm.com
    * @version: 2014年9月5日 下午1:27:25
    */
    public static List<String> getMacIds() {
    InetAddress ip = null;
    NetworkInterface ni = null;
    List<String> macList = new ArrayList<String>();
    try {
    Enumeration<NetworkInterface> netInterfaces = (Enumeration<NetworkInterface>) NetworkInterface
    .getNetworkInterfaces();
    while (netInterfaces.hasMoreElements()) {
    ni = (NetworkInterface) netInterfaces
    .nextElement();
    // ----------特定情况,可以考虑用ni.getName判断
    // 遍历所有ip
    Enumeration<InetAddress> ips = ni.getInetAddresses();
    while (ips.hasMoreElements()) {
    ip = (InetAddress) ips.nextElement();
    if (!ip.isLoopbackAddress() // 非127.0.0.1
    && ip.getHostAddress().matches(
    "(\\d{1,3}\\.){3}\\d{1,3}")) {
    macList.add(getMacFromBytes(ni.getHardwareAddress()));
    }
    }
    }
    } catch (Exception e) {
    OutUtil.error(IpUtil.class, e.getMessage());
    }
    return macList;
    }
    private static String getMacFromBytes(byte[] bytes) {
    StringBuffer mac = new StringBuffer();
    byte currentByte;
    boolean first = false;
    for (byte b : bytes) {
    if (first) {
    mac.append("-");
    }
    currentByte = (byte) ((b & 240) >> 4);
    mac.append(Integer.toHexString(currentByte));
    currentByte = (byte) (b & 15);
    mac.append(Integer.toHexString(currentByte));
    first = true;
    }
    return mac.toString().toUpperCase();
    }

     

    转载于:https://www.cnblogs.com/zxci/p/6248902.html

    展开全文
  • public static List<String> getIp4Addresses() throws SocketException { final List<NetworkInterface> is = getNonLoopBackInterfaces(); return is.stream().flatMap(i -> { final Enumeration<InetAddress>...
        
    public class NetworkInterfaceUtil {
        private static List<NetworkInterface> getInterfaces() throws SocketException {
            return Collections.list(NetworkInterface.getNetworkInterfaces());
        }
    
        private static List<NetworkInterface> getNonLoopBackInterfaces() throws SocketException {
            return getInterfaces().stream()
                                  .filter(i -> Unchecked.supplier(() -> !i.isLoopback()).get())
                                  .collect(toList());
        }
    
        public static List<String> getIp4Addresses() throws SocketException {
            final List<NetworkInterface> is = getNonLoopBackInterfaces();
            return is.stream().flatMap(i -> {
                final Enumeration<InetAddress> addresses = i.getInetAddresses();
                final Builder<String> builder = Stream.builder();
                while (addresses.hasMoreElements()) {
                    final InetAddress ip = addresses.nextElement();
                    if (!ip.isLoopbackAddress()) {
                        if (ip.getHostAddress().equalsIgnoreCase("127.0.0.1")) {
                            continue;
                        }
                        if (ip instanceof Inet6Address) {
                            continue;
                        }
                        if (ip instanceof Inet4Address) {
                            builder.add(ip.getHostAddress());
                        }
                    }
                }
                return builder.build();
            }).collect(toList());
        }
    }

    write on 2017-1-4

    展开全文
  • try { Enumeration... hostIP=ia.getHostAddress(); } } } } catch (SocketException e) { e.printStackTrace(); }   转载于:https://www.cnblogs.com/chaojibaidu/p/10965992.html
    try {
                Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
                while (networkInterfaces.hasMoreElements()) {
                    NetworkInterface ni = (NetworkInterface) networkInterfaces.nextElement();
                    Enumeration<InetAddress> nias = ni.getInetAddresses();
                    while (nias.hasMoreElements()) {
                        InetAddress ia = (InetAddress) nias.nextElement();
                        if (!ia.isLinkLocalAddress() && !ia.isLoopbackAddress() && ia instanceof Inet4Address) {
                            hostIP=ia.getHostAddress();
                        }
                    }
                }
            } catch (SocketException e) {
            	e.printStackTrace();
            }
    

      

    转载于:https://www.cnblogs.com/chaojibaidu/p/10965992.html

    展开全文
  • * desc:获取服务器Ipv4地址 * */ public static String getInet4Address() { Enumeration<NetworkInterface> nis; String ip = null; try { nis = NetworkInterface...
  • 主要介绍了java编程实现获取机器IP地址及MAC地址的方法,实例分析了Java分别针对单网卡及多网卡的情况下获取服务器IP地址与MAC地址的相关技巧,需要的朋友可以参考下
  • 本文实例讲述了java编程实现获取服务器IP地址及MAC地址的方法。分享给大家供大家参考,具体如下: 已测系统: windows linux unix 排除127.0.0.1 和 0.0.0.0.1等非正常IP import java.net.InetAddress; import ...
  • Java获取服务器IP

    2019-07-11 11:15:07
    * 获取服务器IP地址 * @return */ public String getHostAddress() { String ret = ""; try { InetAddress address = InetAddress.getLocalHost(); ret = address.getHostAddress(); } catch (.....
  • @Resource  private WebServiceContext webServiceContext;  public String getLoginUser() throws Exception {  InetAddress ip;  try {  ip = InetAddress.getLocalHost();  ...
  • 因为同一环境有多台服务器,为了快速排查问题希望在邮件标题上增加机器的IP。 实现 import lombok.extern.slf4j.Slf4j; import java.net.InetAddress; import java.net.NetworkInterface; import java.net....
  • java获取本地IP和服务器IP

    千次阅读 2018-09-19 10:21:03
    java获取IP地址 获取本地IP地址 //这种方法能不能在Linux服务器上直接获取IP有待验证 String address = InetAddress.... 获取服务器IP地址(针对nginx) package com.utils; import javax.servlet.http.Ht...
  • 一、Java获取服务器IP(转自:https://www.cnblogs.com/george93/p/6306579.html) //方法一,也是最简单的一种。 String SERVER_IP = InetAddress.getLocalHost().getHostAdress().... * 获取服务器IP地址 ...
  • HttpServletRequest httpRequest=(HttpServletRequest)request;...String strBackUrl = "http://" + request.getServerName() //服务器地址 + ":" + request.getServerPort() //端口号 + httpRequest.
  • Java 获取客户端ip地址

    2019-05-02 22:54:13
    目录java获取客户端ip java获取客户端ip 客户端所有的请求信息都可以通过HttpServletRequest获得,获取客户端ip可以通过getRemoteAddr()获取。 但是如果客户端是通过代理访问我们的服务器,那么getRemoteAddr()获取...
  • 想了解java获取linux服务器上的IP操作的相关内容吗,gzy_0412在本文为您仔细讲解java linux服务器IP的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:java,linux服务器,IP,下面大家一起来学习吧。...
  • IP地址获取】java获取客户端ip地址

    万次阅读 2018-08-31 16:44:38
    如何获取客户端ip和url访问中服务器端ip(如果是ip,不是域名)呢? String requestUrlIP = request.getServerName(); ...***访问的Url中的服务器IP:"+requestUrlIP); System.out.print...
  • 不传参数,直接通过ServletRequestAttributes获取当前项目所在服务器的端口号和ip地址 public static String getUrl(){ ServletRequestAttributes requestAttributes = (ServletRequestAttributes) ...
  • 获取当前服务器IP地址 import java.net.*; import java.util.*; //获取当前服务器IP public class IPdemo { public static void main(String[] args) { System.out.println(getMyIp()); } @SuppressWarnings...
  • * 获取本机IP */ public static String getLocalIP() { String ip = ""; try { if(isLinux()){ Enumeration<?> e1 = (Enumeration<?>) NetworkInterface .getNetworkInterfaces();
  • Java获取当前服务器所有的IP地址 此代码会列出服务器所有的IP地址,包括IPv4和IPv6的 @Test public void s() throws SocketException { Enumeration<NetworkInterface> e = NetworkInterface....
  • 文章目录1.方法 1.方法 ... String ip=""; InetAddress inetAddress = null; try { Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces(); whil
  • 1、获取Tomcat端口 MBeanServer beanServer = ManagementFactory.getPlatformMBeanServer(); Set<ObjectName> objectNames = null; try { objectNames = beanServer .queryNames(new ObjectName("*:type=...
  • Java 获取真实IP地址

    2016-12-29 15:02:29
    当项目发布在内网的时候,主机(如IP地址为192.168.10.88)访问局域网服务器的url,如http://192.168.10.142:8080/index/,用request.getRemoteAddr()获取到的主机IP地址是192.168.10.142,而不是真实的IP地址192....

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 675
精华内容 270
关键字:

java获取服务器ip地址

java 订阅