zoukankan      html  css  js  c++  java
  • java在Win7 64位 获取客户端的IP,MAC,计算机名

    package com.javaweb.util;
    
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.LineNumberReader;
    
    import javax.servlet.http.HttpServletRequest;
    
    public class ClientInformation {
    //得到客户端IP地址
    public static 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.getHeader("HTTP_CLIENT_IP");
    }
    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
    ip = request.getHeader("HTTP_X_FORWARDED_FOR");
    }
    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
    ip = request.getRemoteAddr();
    }
    return ip;
    }
    
    //得到客户端MAC地址
    public static String getMACAddress(String ip) {
    String str = "";
    String macAddress = "";
    System.out.println("ipppppppppppppppppp"+ip);
    try {
    Process p = Runtime.getRuntime().exec("cmd /c C:\Windows\sysnative\nbtstat.exe -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") > 1) {
    macAddress = str.substring(str.indexOf("=") + 2,
    str.length());
    break;
    }
    }
    }
    } catch (IOException e) {
    e.printStackTrace(System.out);
    }
    return macAddress;
    }
    
    //得到客户端计算机名
    public static String getComputerName(String ip){
    String computerName = "";
    String str = "";
    try {
    Process    p = Runtime.getRuntime().exec("cmd /c C:\Windows\sysnative\nbtstat.exe -a " + ip);
    InputStreamReader ir = new InputStreamReader(p.getInputStream());
    LineNumberReader input = new LineNumberReader(ir);
    for (int i = 1; i < 100; i++) {
    try {
    str = input.readLine();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    if (str.indexOf("唯一") > 1) {
    computerName = str.substring(0, str.indexOf("<")).trim();
    break;
    }
    }
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    return computerName;
    }
    }

     

  • 相关阅读:
    Python的函数式编程: map, reduce, sorted, filter, lambda
    idea cant resolve symbo 'Table'
    idea离线下载lombok,以及lobok版本不兼容
    idea 设置author 设置黑色主题
    sbmvnmysql配置
    vue.js 接收url参数
    简单商城的数据库建表sql
    vue项目从静态页面添加后台出现的一些问题
    HTML转义字符大全
    vue dialog样式
  • 原文地址:https://www.cnblogs.com/Wu-W-Sen/p/4270160.html
Copyright © 2011-2022 走看看