java获取ip地址和Mac地址

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值