博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Selvet获取客户端ip地址和mac地址
阅读量:5932 次
发布时间:2019-06-19

本文共 2774 字,大约阅读时间需要 9 分钟。

hot3.png

package com.babyDuncan.java;  import java.io.IOException;  import java.io.InputStreamReader;  import java.io.LineNumberReader;  import java.io.PrintWriter;    import javax.servlet.ServletException;  import javax.servlet.http.HttpServlet;  import javax.servlet.http.HttpServletRequest;  import javax.servlet.http.HttpServletResponse;    public class ipServlet 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));            response.setContentType("text/html");          PrintWriter out = response.getWriter();          out.println("");          out.println("");          out.println("  A Servlet");          out.println("  ");          out.print("    This is ");          out.print(this.getClass());          out.println(", using the POST method");          out.println("  ");          out.println("");          out.flush();          out.close();      }        public String getRemoteAddress(HttpServletRequest request) {          String ip = request.getHeader("x-forwarded-for");          if (ip == null || ip.length() == 0 || ip.equalsIgnoreCase("unknown")) {              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);              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;      }  }

转载于:https://my.oschina.net/u/1427708/blog/710623

你可能感兴趣的文章
AngularJs ng-route路由详解
查看>>
数据结构-王道-图
查看>>
3.6版多线程
查看>>
ionic使用方法
查看>>
20172303 2018-2019-1 《程序设计与数据结构》第6周课堂实践报告
查看>>
让数据库变快的10个建议
查看>>
android集成twitter登录
查看>>
美化 Linux 字体显示
查看>>
java的继承
查看>>
File操作
查看>>
pl/sql 存储结构
查看>>
压缩,解压缩工具及bash脚本编程
查看>>
Jemeter第一个实例
查看>>
架构之美阅读笔记五
查看>>
Agile.Net 组件式开发平台 - 组件开发示例
查看>>
Linux 脚本的一个尝试
查看>>
CSS 兼容性和 Internet Explorer
查看>>
configparser模块
查看>>
【ORA-12560: TNS: 协议适配器错误】解决办法总结
查看>>
《Oracle大型数据库在AIX UNIX上的实战详解》的集中讨论十一 AIX条带化、存储条带化、ASM条带化...
查看>>