JAVAWEB获取用户IP地址、用户所在地、物理地址(MAC)

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/u011028655/article/details/40685859

获取用户IP地址、用户所在地

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>GetIpInfor</title>


<script type="text/javascript" src="http://counter.sina.com.cn/ip/" charset="gb2312"></script>
<script type="text/javascript" > 
function GetIpInfor(){
document.getElementById("ip").value=ILData[0];           //得到接口数据中的IP地址并传值给id为ip的控件   
document.getElementById("country").value=ILData[1];      //得到接口数据中的IP地址的国家信息并传值  
document.getElementById("province").value=ILData[2];     //输出接口数据中的IP地址的省份信息并传值  
document.getElementById("city").value=ILData[3];         //输出接口数据中的IP地址的城市信息并传值
document.getElementById("area").value=ILData[4];         //输出接口数据中的IP地址的运营商信息并传值
}      
</script>
<body onLoad="GetIpInfor();"> <!-- 页面加载时候响应GetIpInfor()函数-->


	
		
		<input type="text" id="ip" value="" /> <input type="text"
			id="country" value="" /> <input type="text" id="province" value="" />
		<input type="text" id="city" value="" /> <input type="text"
			id="area" value="" />
	
	
</body>

</html>

获取用户物理地址(即MAC地址)

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="java.io.InputStreamReader" %>
<%@page import="java.io.LineNumberReader" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'MyJsp.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    <%
    String str = "";
		String MAC = "";
		
			Process process = Runtime.getRuntime().exec("cmd.exe /c ipconfig /all");
			InputStreamReader isr = new InputStreamReader(process.getInputStream());
			LineNumberReader lnr= new LineNumberReader(isr);
			for (int i = 1; i < 100; i++) {
				str = lnr.readLine();
				if (str != null) {
					int index1 = str.indexOf("Physical Address");
					int index2 = str.indexOf("物理地址");
					if (index1 > 1) {
						MAC = str.substring(str.indexOf(":") + 2, str.length());
						break;
					}
					if (index2 > 1) {
						MAC = str.substring(str.indexOf(":") + 2, str.length());
						break;
					}
				}
			}
		
     %>
     <%=MAC %>
  </body>
</html>


展开阅读全文

没有更多推荐了,返回首页