获取设备信息——获取客户端ip地址和mac地址

1、获取本地IP(有可能是 内网IP,192.168.xxx.xxx)

 /**
     * 获取本地IP
     *
     * @return
     */
    public static String getLocalIpAddress() {
        try {
            Enumeration<networkinterface> en = NetworkInterface
                    .getNetworkInterfaces();
            while (en.hasMoreElements()) {
                NetworkInterface ni = en.nextElement();
                Enumeration<inetaddress> enIp = ni.getInetAddresses();
                while (enIp.hasMoreElements()) {
                    InetAddress inet = enIp.nextElement();
                    if (!inet.isLoopbackAddress()
                            && (inet instanceof Inet4Address)) {
                        return inet.getHostAddress().toString();
                    }
                }
            }
        } catch (SocketException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return "0";
    }</inetaddress></networkinterface>

2.获取网络ip,比上述方法更加精确,但是要异步执行。

/**
     * 获取外网的IP(要访问Url,要放到后台线程里处理)
     *
     * @Title: GetNetIp
     * @Description:
     * @param @return
     * @return String
     * @throws
     */
    public static String GetNetIp() {
        URL infoUrl = null;
        InputStream inStream = null;
        String ipLine = "";
        HttpURLConnection httpConnection = null;
        try {
            infoUrl = new URL("https://ip168.com/");
            URLConnection connection = infoUrl.openConnection();
            httpConnection = (HttpURLConnection) connection;
            int responseCode = httpConnection.getResponseCode();
            if (responseCode == HttpURLConnection.HTTP_OK) {
                inStream = httpConnection.getInputStream();
                BufferedReader reader = new BufferedReader(
                        new InputStreamReader(inStream, "utf-8"));
                StringBuilder strber = new StringBuilder();
                String line = null;
                while ((line = reader.readLine()) != null)
                    strber.append(line + "\n");

                Pattern pattern = Pattern
                        .compile("((?:(?:25[0-5]|2[0-4]\\d|((1\\d{2})|([1-9]?\\d)))\\.){3}(?:25[0-5]|2[0-4]\\d|((1\\d{2})|([1-9]?\\d))))");
                Matcher matcher = pattern.matcher(strber.toString());
                if (matcher.find()) {
                    ipLine = matcher.group();
                }
            }

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                inStream.close();
                httpConnection.disconnect();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return ipLine;
    }

3.获取mac地址

public static String getLocalMacAddress() {//没有缓存的地址,则查询
        String mac_s = "";
        try {
            byte[] mac;
            NetworkInterface ne = NetworkInterface.getByInetAddress(InetAddress.getByName(getLocalIpAddress()));
            mac = ne.getHardwareAddress();
            mac_s = byte2hex(mac);
        } catch (Exception e) {
        }
        mac_s;
        return mac_s;
    }
时间: 2024-08-26 10:51:03

获取设备信息——获取客户端ip地址和mac地址的相关文章

java获取客户端IP地址和MAC地址

最近项目中要获得客户端的mac地址.服务端是移植的centos系统,arm架构的盒子.客户端是手机和移动设备.(其它场景应该也是类似的) 首先要获得ip地址: 根据客户端的http请求,利用request.getRemoteAddr()获取客户端Ip地址.在局域网内getRemoteAddr()和getRemoteHost()获得的结果相同.request.getRemoteAddr()是获得客户端的ip地址 .getRemoteHost()是获得客户端的主机名 .在有些场景中,可能有Squid

获取客户端的ip地址与mac地址总结

最近刚完成的一个模块中,需要获取系统客户端的IP地址与物理地址(MAC地址). 1. 获取的本机IP与MAC是服务器的,而非客户端的→_→ 通过JAVA获取,本机的IP地址与MAC地址,使用如下代码即可完成: package com.howin.util; import java.net.*; public class Ipconfig { public static void main(String[] args) throws Exception { // TODO Auto-generat

获取本机的IP地址和mac地址

1. 以前一直用ipconfig来查看ip地址,哈哈哈,现在发现挺好玩 #获取本机的IP地址和mac地址 import uuid import socket def get_mac_address(): mac=uuid.UUID(int = uuid.getnode()).hex[-12:] #print(mac) return ":".join([mac[e:e+2] for e in range(0,11,2)]) #range(0,11,2):在[0:11]取值,间隔为2 de

一句话脚本系列之获取eth0网卡的IP地址(或MAC地址)

欢迎转载!转载时请注明出处:http://blog.csdn.net/nfer_zhuang/article/details/42609733 引言 使用场景: 我使用的Linux服务器上有多个网卡,有时候就需要在脚本中使用到指定网卡的IP地址或MAC地址,那么这个时候就有两种做法,一是预先读取网卡的IP地址或MAC地址,然后写入到脚本中:另外一种方法就是通过shell命令实时获取指定网卡上的当前IP地址或MAC地址. 一般用法: 使用ifconfig eth0读取网卡信息 手动将IP地址或MA

获取本机IP地址和MAC地址

unit NetFunc; interface uses SysUtils, Windows, dialogs, winsock, Classes, ComObj, WinInet, Variants; //错误信息常量 const C_Err_GetLocalIp = '获取本地ip失败'; C_Err_GetNameByIpAddr = '获取主机名失败'; C_Err_GetSQLServerList = '获取SQLServer服务器失败'; C_Err_GetUserResource

Flutter 中获取设备信息 以及 获取地 理位置

一.Flutter 中获取设备信息 https://pub.dev/packages/device_info 设备信息代码 import 'package:flutter/material.dart';import 'package:device_info/device_info.dart'; class DevicPage extends StatefulWidget{ DevicPage({Key key}); _DevicPage createState() => _DevicPage()

PhoneGap获取设备信息

一. 获取设备信息的方法列表(如果没有或者检测不出来就显示undefined) 1.device.name              设备名称(一些国产机检测不出来) 2.device.model     :返回设备的模型或产品的名称 3.device.cordova           PhoneGap 版本 4.device.platform         设备平台 5.device.uuid               uuid 编码(这个编码是设备的唯一编码相当于每部手机的身份证,比较

微信小程序把玩(三十八)获取设备信息 API

原文:微信小程序把玩(三十八)获取设备信息 API 获取设备信息这里分为四种, 主要属性: 网络信息wx.getNetWorkType, 系统信息wx.getSystemInfo, 重力感应数据wx.onAccelerometerChange, 罗盘数据wx.onCompassChange wxml <button type="primary" bindtap="getNetWorkType">获取网络类型</button> <butt

获取访问用户的客户端IP(适用于公网与局域网).

/**   * 获取访问用户的客户端IP(适用于公网与局域网).   */   public final String getIpAddr(final HttpServletRequest request)           throws Exception {       if (request == null) {           throw (new Exception("getIpAddr method HttpServletRequest Object is null"