获取 请求方 mac 地址
来源:互联网 发布:游戏宣传片制作软件 编辑:程序博客网 时间:2024/05/16 01:03
获取 请求方 mac 地址
package cn.com.henry.servlet;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintWriter;import java.net.InetAddress;import java.net.NetworkInterface;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class Test extends HttpServlet {/** * Constructor of the object. */public Test() {super();}/** * Destruction of the servlet. <br> */public void destroy() {super.destroy(); // Just puts "destroy" string in log// Put your code here}/** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {response.setContentType("text/html");PrintWriter out = response.getWriter();out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");out.println("<HTML>");out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");out.println(" <BODY>");try {out.println("------------------------------------");out.println("ip:"+getIpAddr(request));} catch (Exception e) {e.printStackTrace();}try {out.println("mac:"+getMACAddress(getIpAddr(request)));out.println("------------------------------------");} catch (Exception e) {e.printStackTrace();}out.print(" This is ");out.print(this.getClass());out.println(", using the GET method");out.println(" </BODY>");out.println("</HTML>");out.flush();out.close();}/** * The doPost method of the servlet. <br> * * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {response.setContentType("text/html");PrintWriter out = response.getWriter();out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");out.println("<HTML>");out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");out.println(" <BODY>");try {out.println("------------------------------------");out.println("ip:"+getIpAddr(request));} catch (Exception e) {e.printStackTrace();}try {out.println("mac:"+getMACAddress(getIpAddr(request)));out.println("------------------------------------");} catch (Exception e) {e.printStackTrace();}out.print(" This is ");out.print(this.getClass());out.println(", using the POST method");out.println(" </BODY>");out.println("</HTML>");out.flush();out.close();} /** * 通过HttpServletRequest返回IP地址 * @param request HttpServletRequest * @return ip String * @throws Exception */ public String getIpAddr(HttpServletRequest request) throws Exception { 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; } /** * 通过IP地址获取MAC地址 * @param ip String,127.0.0.1格式 * @return mac String * @throws Exception */ public String getMACAddress(String ip) throws Exception { String line = ""; String macAddress = ""; final String MAC_ADDRESS_PREFIX = "MAC Address = "; final String LOOPBACK_ADDRESS = "127.0.0.1"; //如果为127.0.0.1,则获取本地MAC地址。 if (LOOPBACK_ADDRESS.equals(ip)) { InetAddress inetAddress = InetAddress.getLocalHost(); //貌似此方法需要JDK1.6。 byte[] mac = NetworkInterface.getByInetAddress(inetAddress).getHardwareAddress(); //下面代码是把mac地址拼装成String StringBuilder sb = new StringBuilder(); for (int i = 0; i < mac.length; i++) { if (i != 0) { sb.append("-"); } //mac[i] & 0xFF 是为了把byte转化为正整数 String s = Integer.toHexString(mac[i] & 0xFF); sb.append(s.length() == 1 ? 0 + s : s); } //把字符串所有小写字母改为大写成为正规的mac地址并返回 macAddress = sb.toString().trim().toUpperCase(); return macAddress; } //获取非本地IP的MAC地址 try { Process p = Runtime.getRuntime().exec("nbtstat -A " + ip); InputStreamReader isr = new InputStreamReader(p.getInputStream()); BufferedReader br = new BufferedReader(isr); while ((line = br.readLine()) != null) { if (line != null) { int index = line.indexOf(MAC_ADDRESS_PREFIX); if (index != -1) { macAddress = line.substring(index + MAC_ADDRESS_PREFIX.length()).trim().toUpperCase(); } } } br.close(); } catch (IOException e) { e.printStackTrace(System.out); } return macAddress; } }
- 获取 请求方 mac 地址
- javaweb项目 UDP发送请求获取客户端MAC地址
- 通过arp请求获取苹果手机的mac地址
- 简述如何获取请求方的IP地址,及php相关获取函数解析
- android 6.0 第三方库获取无法获取设备Mac地址
- 获取请求URL地址
- 获取请求ip地址
- 获取请求IP地址
- 获取请求IP地址
- 获取MAC地址
- 获取Mac地址
- 获取Mac地址
- 获取MAC地址
- 获取MAC地址(Err)
- 获取网卡MAC地址
- 获取客户端MAC地址
- 获取mac地址
- 获取客户端mac地址
- JS中的字符串的trim()方法
- 未能从程序集“mscorlib,Version=2.0.0.0,Culture=neutral, PublicKeyToken=b77a5c561934e089”中加载类型“System.DateTi
- wince MFC调试问题总结
- C++ 智能指针
- 【二分图匹配】 hdu3605 Escape
- 获取 请求方 mac 地址
- 安装两个BCB6控件SynEdit、mwEdit 0.92a的过程总结
- hdu_4180 RealPhobia (扩展欧几里德)
- sicily--1306. Sorting Algorithm
- 在iOS4中使用代码块
- C++中 string 的使用
- [bfs] poj 1753 Flip Game# bfs + 位存储
- 计算机网络层(3)--------《计算机网络-自顶向下方法》
- ACM水题-数据选取(AC解题报告)