您的位置:首页 > 编程语言 > Java开发

java获取ip地址和Mac地址

2015-12-31 09:45 543 查看
public class GetMacAddress extends HttpServlet{

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

String ip = getRemoteAddress(request);
System.out.println(ip);
System.out.println(getMACAddress(ip));

}

public String getRemoteAddress(HttpServletRequest request) {
String ip = request.getHeader("x-forwarded-for");
if (ip == null || ip.length() == 0 || ip.equalsIgnoreCase("unknown")) {      //这里主要是为了防止出现代理或其他原因而的不到真正的IP地址;
ip = request.getHeader("Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || ip.equalsIgnoreCase("unknown")) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || ip.equalsIgnoreCase("unknown")) {
ip = request.getRemoteAddr();
}
return ip;
}

public String getMACAddress(String ip) {
String str = "";
String macAddress = "";
try {
Process p = Runtime.getRuntime().exec("nbtstat -A " + ip);     //相当于执行cdm命令
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;
}
}
}
} catch (IOException e) {
e.printStackTrace(System.out);
}
return macAddress;
}

}

标签: java物理地址 java网卡地址 定位电脑 杂谈分类: 程序之家

    在JAVA中有时候会需要定位到某台电脑,而通过IP定位显然是不行的,有一种解决方案是通过定位到电脑的物理地址来定位电脑,这是本文要介绍的内容。    1. PhysicalAddressUtilpackage com.jetsum.web.util;import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.util.logging.Level;

import java.util.logging.Logger;import test.Test;

public class PhysicalAddress

{

 public static String getMACAddress()

 {  String address = "";

  String os = System.getProperty("os.name");

  if (os != null)

  {

   if (os.startsWith("Windows"))

   {

    try

    {

     ProcessBuilder pb = new ProcessBuilder("ipconfig", "/all");

     Process p = pb.start();

     BufferedReader br = new BufferedReader(

       new InputStreamReader(p.getInputStream()));

     String line;

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

     {

      if (line.indexOf("Physical Address") != -1)

      {

       int index = line.indexOf(":");

       address = line.substring(index + 1);

       break;

      }

     }

     br.close();

     return address.trim();

    } catch (IOException e)

    {    }

   } else if (os.startsWith("Linux"))

   {

    try

    {

     ProcessBuilder pb = new ProcessBuilder("ifconfig");

     Process p = pb.start();

     BufferedReader br = new BufferedReader(

       new InputStreamReader(p.getInputStream()));

     String line;

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

     {

      int index = line.indexOf("硬件地址");

      if (index != -1)

      {

       address = line.substring(index + 4);

       break;

      }

     }

     br.close();

     return address.trim();

    } catch (IOException ex)

    {

     Logger.getLogger(Test.class.getName()).log(Level.SEVERE,

       null, ex);

    }   }

  }

  return address;

 }

}

    2.使用String address = PhysicalAddressUtil.getMACAddress() ;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: