「java获取ipmac」JAVA获取当前月份

博主:adminadmin 2022-11-25 03:28:07 58

本篇文章给大家谈谈java获取ipmac,以及JAVA获取当前月份对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

java如何查询本机ip地址和mac地址

// 获取mac地址

  public static String getMacAddress() {

    try {

      EnumerationNetworkInterface allNetInterfaces = NetworkInterface.getNetworkInterfaces();

      byte[] mac = null;

      while (allNetInterfaces.hasMoreElements()) {

        NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();

        if (netInterface.isLoopback() || netInterface.isVirtual() || !netInterface.isUp()) {

          continue;

        } else {

          mac = netInterface.getHardwareAddress();

          if (mac != null) {

            StringBuilder sb = new StringBuilder();

            for (int i = 0; i  mac.length; i++) {

              sb.append(String.format("%02X%s", mac[i], (i  mac.length - 1) ? "-" : ""));

            }

            if (sb.length()  0) {

              return sb.toString();

            }

          }

        }

      }

    } catch (Exception e) {

      _logger.error("MAC地址获取失败", e);

    }

    return "";

  }

 

  // 获取ip地址

  public static String getIpAddress() {

    try {

      EnumerationNetworkInterface allNetInterfaces = NetworkInterface.getNetworkInterfaces();

      InetAddress ip = null;

      while (allNetInterfaces.hasMoreElements()) {

        NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();

        if (netInterface.isLoopback() || netInterface.isVirtual() || !netInterface.isUp()) {

          continue;

        } else {

          EnumerationInetAddress addresses = netInterface.getInetAddresses();

          while (addresses.hasMoreElements()) {

            ip = addresses.nextElement();

            if (ip != null  ip instanceof Inet4Address) {

              return ip.getHostAddress();

            }

          }

        }

      }

    } catch (Exception e) {

      _logger.error("IP地址获取失败", e);

    }

    return "";

  }

希望能帮助到你

求获取客户端mac地址java代码,急需急需,麻烦了

通过ip获取指定ip地址的mac地址,ip可以通过请求request获取,

request.getRemoteAddr();

(当然获取ip也不是在任何情况下都有效的)

通过了Apache,Squid等反向代理软件就不能获取到客户端的真实IP地址。

您也许需要通过其他的方式获取,(见附)

//获取mac如下 (nbtstat -A IPAddress是对给定的IP地址解析其主机名。如果不能正常解析它的主机

//名的话,有可能是防火墙屏蔽了。也可能是在DNS中将NetBios 解析选项屏蔽了。)

public String getMACAddress(String ip){

String str = "";

String macAddress = "";

try {

Process p = Runtime.getRuntime().exec("nbtstat -A " + ip);

InputStreamReader ir = new InputStreamReader(p.getInputStream());

LineNumberReader input = new LineNumberReader(ir);

for (int i = 1; i 100; i++) {

str = input.readLine();

if (str != null) {

if (str.indexOf("MAC Address") 1) {

macAddress = str.substring(str.indexOf("MAC Address") + 14, str.length());

break;

}

if (str.indexOf("MAC Address") 1) {

macAddress = str.substring(str.indexOf("MAC 地址") + 14, str.length());

break;

}

//以上有个判断,不同系统cmd命令执行的返回结果展示方式不一样,我测试的win7是MAC 地址

//所以又第二个if判断 你可先在你机器上cmd测试下nbtstat -A 命令 当然得有一个你可以ping通的

//网络ip地址,然后根据你得到的结果中mac地址显示方式来确定这个循环取值

}

}

} catch (IOException e) {

e.printStackTrace(System.out);

}

return macAddress;

}

附:

通过代理了的客户端ip地址获取方式

于是可得出获得客户端真实IP地址的方法一:

public String getRemortIP(HttpServletRequest request) {

if (request.getHeader("x-forwarded-for") == null) {

return request.getRemoteAddr();

}

return request.getHeader("x-forwarded-for");

}

获得客户端真实IP地址的方法二:

public String getIpAddr(HttpServletRequest request) {

String ip = request.getHeader("x-forwarded-for");

if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {

ip = request.getHeader("Proxy-Client-IP");

}

if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {

ip = request.getHeader("WL-Proxy-Client-IP");

}

if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {

ip = request.getRemoteAddr();

}

return ip;

}

可是,如果通过了多级反向代理的话,X-Forwarded-For的值并不止一个,而是一串IP值,究竟哪个才是真正的用户端的真实IP呢?

答案是取X-Forwarded-For中第一个非unknown的有效IP字符串。如:

X-Forwarded-For:192.168.1.110, 192.168.1.120, 192.168.1.130, 192.168.1.100

用户真实IP为: 192.168.1.110

java 怎样利用IP地址获得局域网计算机的名字、mac地址、工作组?

指定IP的MAC

代码如下:

Java

code

System.out.println("192.168.1.187对应网卡的MAC是:");NetworkInterface

ne=NetworkInterface.getByInetAddress(InetAddress.getByName("192.168.1.187"));byte[]mac=ne.getHardwareAddress();String

mac_s=hexByte(mac[0])+":"+hexByte(mac[1])+":"+

hexByte(mac[2])+":"+hexByte(mac[3])+":"+

hexByte(mac[4])+":"+hexByte(mac[5]);System.out.println(mac_s);

程序运行结果:

192.168.1.187对应网卡的MAC是:

00:0c:f1:20:75:58

工作组和

计算机

名字类似,可以到库里找

JAVA如何获取客户端IP地址和MAC地址

/**

* 获取IP地址

* @return

*/

public static String GetNetIp() {

URL infoUrl = null;

InputStream inStream = null;

String line = "";

try {

infoUrl = new URL("");

URLConnection connection = infoUrl.openConnection();

HttpURLConnection 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();

while ((line = reader.readLine()) != null)

strber.append(line + "\n");

inStream.close();

// 从反馈的结果中提取出IP地址

int start = strber.indexOf("{");

int end = strber.indexOf("}");

String json = strber.substring(start, end + 1);

if (json != null) {

try {

JSONObject jsonObject = new JSONObject(json);

line = jsonObject.optString("cip");

} catch (JSONException e) {

e.printStackTrace();

}

}

return line;

}

} catch (MalformedURLException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

return line;

}

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;

}

java如何不使用HttpServletRequest获取电脑客户端ip地址与Mac地址。

import java.net.InetAddress;

import java.net.NetworkInterface;

/**

*@author:

*@version:

*@description:

*/

public class Ipconfig {

public static void main(String[] args) throws Exception {

try {

InetAddress ia=InetAddress.getLocalHost();

String localname=ia.getHostName();

String localip=ia.getHostAddress();

System.out.println("本机名称是:"+ localname);

System.out.println("本机的ip是 :"+localip);

System.out.println("MAC ......... "+getMACAddress(ia));

} catch (Exception e) {

e.printStackTrace();

}

}

//获取MAC地址的方法

private static String getMACAddress(InetAddress ia)throws Exception{

//获得网络接口对象(即网卡),并得到mac地址,mac地址存在于一个byte数组中。

byte[] mac = NetworkInterface.getByInetAddress(ia).getHardwareAddress();

//下面代码是把mac地址拼装成String

StringBuffer sb = new StringBuffer();

for(int i=0;imac.length;i++){

if(i!=0){

sb.append("-");

}

//mac[i] 0xFF 是为了把byte转化为正整数

String s = Integer.toHexString(mac[i] 0xFF);

sb.append(s.length()==1?0+s:s);

}

//把字符串所有小写字母改为大写成为正规的mac地址并返回

return sb.toString().toUpperCase();

}

}

关于java获取ipmac和JAVA获取当前月份的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

The End

发布于:2022-11-25,除非注明,否则均为首码项目网原创文章,转载请注明出处。