Servlet获取客户端ip地址和mac地址

转自:http://babyduncan.iteye.com/blog/892351
Java代码   收藏代码
  1. package com.babyDuncan.java;  
  2.   
  3. import java.io.IOException;  
  4. import java.io.InputStreamReader;  
  5. import java.io.LineNumberReader;  
  6. import java.io.PrintWriter;  
  7.   
  8. import javax.servlet.ServletException;  
  9. import javax.servlet.http.HttpServlet;  
  10. import javax.servlet.http.HttpServletRequest;  
  11. import javax.servlet.http.HttpServletResponse;  
  12.   
  13. public class ipServlet extends HttpServlet {  
  14.   
  15.     public void doGet(HttpServletRequest request, HttpServletResponse response)  
  16.             throws ServletException, IOException {  
  17.         doPost(request, response);  
  18.     }  
  19.   
  20.     public void doPost(HttpServletRequest request, HttpServletResponse response)  
  21.             throws ServletException, IOException {  
  22.   
  23.         String ip = getRemoteAddress(request);  
  24.         System.out.println(ip);  
  25.         System.out.println(getMACAddress(ip));  
  26.   
  27.         response.setContentType("text/html");  
  28.         PrintWriter out = response.getWriter();  
  29.         out  
  30.                 .println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");  
  31.         out.println("<HTML>");  
  32.         out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");  
  33.         out.println("  <BODY>");  
  34.         out.print("    This is ");  
  35.         out.print(this.getClass());  
  36.         out.println(", using the POST method");  
  37.         out.println("  </BODY>");  
  38.         out.println("</HTML>");  
  39.         out.flush();  
  40.         out.close();  
  41.     }  
  42.   
  43.     public String getRemoteAddress(HttpServletRequest request) {  
  44.         String ip = request.getHeader("x-forwarded-for");  
  45.         if (ip == null || ip.length() == 0 || ip.equalsIgnoreCase("unknown")) {  
  46.             ip = request.getHeader("Proxy-Client-IP");  
  47.         }  
  48.         if (ip == null || ip.length() == 0 || ip.equalsIgnoreCase("unknown")) {  
  49.             ip = request.getHeader("WL-Proxy-Client-IP");  
  50.         }  
  51.         if (ip == null || ip.length() == 0 || ip.equalsIgnoreCase("unknown")) {  
  52.             ip = request.getRemoteAddr();  
  53.         }  
  54.         return ip;  
  55.     }  
  56.   
  57.     public String getMACAddress(String ip) {  
  58.         String str = "";  
  59.         String macAddress = "";  
  60.         try {  
  61.             Process p = Runtime.getRuntime().exec("nbtstat -A " + ip);  
  62.             InputStreamReader ir = new InputStreamReader(p.getInputStream());  
  63.             LineNumberReader input = new LineNumberReader(ir);  
  64.             for (int i = 1; i < 100; i++) {  
  65.                 str = input.readLine();  
  66.                 if (str != null) {  
  67.                     if (str.indexOf("MAC Address") > 1) {  
  68.                         macAddress = str.substring(  
  69.                                 str.indexOf("MAC Address") + 14, str.length());  
  70.                         break;  
  71.                     }  
  72.                 }  
  73.             }  
  74.         } catch (IOException e) {  
  75.             e.printStackTrace(System.out);  
  76.         }  
  77.         return macAddress;  
  78.     }  
  79. }  

访问地址: 
http://hanvon-babydunc:8080/testServlet/servlet/ipServlet 
运行结果: 
192.168.10.18 
00-26-C7-21-65-46 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值