网页获取访问IP,并解析地址

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

 jsp网页

<%@ 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>首页</title>
</head>
<%@ page import="cn.personweb.Dao.*" %>
<%@ page import="cn.personweb.info.*" %>

<%
	//使用方法获取ip
	String ip= GetIPAddress.getIp(request);
	//使用方法获取地址
	String addr = GetIPAddress.getAddressByIP(ip);
	//将获取的ip添加到数据库
	ipDao.insertip("P", ip, addr);
%>
<body>
	<jsp:forward page="personweb/index.jsp"></jsp:forward>
</body>
</html>

获取ip方法,解析地址(添加到数据库方法省略)

package info;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

import javax.servlet.http.HttpServletRequest;
/**
 * 获取ip并解析地址
 * @author WZY
 *
 */
public class GetIPAddress {
	/**
	 * 网页请求获取iP
	 * @param request
	 * @return
	 */
	public static String getIp(HttpServletRequest request) {
		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.getRemoteAddr();
		}
		return ip;
	}
	/**
	 * 根据ip解析地址
	 * @param strIP
	 * @return
	 */
	public static String getAddressByIP(String strIP) {
		String msg = "";
		try {
			URL url = new URL("http://ip.taobao.com/service/getIpInfo.php?ip=" + strIP);
			URLConnection conn = url.openConnection();
			BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
			String line = null;
			StringBuffer result = new StringBuffer();
			while ((line = reader.readLine()) != null) {
				result.append(line);
			}
			reader.close();

			String code = "{\"code\":";
			if ("0".equals(String.valueOf(result.toString().charAt(result.toString().indexOf(code) + code.length())))) {
				String country = ",\"country\":\"";
				msg += getDetail(result.toString(), country);

				String area = ",\"area\":\"";
				msg += getDetail(result.toString(), area);

				String region = ",\"region\":\"";
				msg += getDetail(result.toString(), region);

				String city = ",\"city\":\"";
				msg += getDetail(result.toString(), city);
			} else {
				msg = "无效查询";
			}
		
		} catch (IOException e) {
			msg = "无效查询";
		}
		return msg;
	}
	/**
	 * 获取相应信息
	 * @param str
	 * @param substr
	 * @return
	 */
	public static String getDetail(String str, String substr) {
		String result = " ";
		int substr_index = str.indexOf(substr) + substr.length();
		while (!"\"".equals(String.valueOf(str.charAt(substr_index)))) {
			result += String.valueOf(str.charAt(substr_index));
			substr_index++;
		}
		return result;
	}
}

测试

展开阅读全文

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