經過用戶IP獲取用戶地址信息node
首先獲得ip,有兩種方式json
第一種方式:經過jsoup從一些工具網頁獲取api
第二種方式:經過獲取用戶對象信息獲得app
前者須要jsoup的jar包,不須要用戶請求;
後者須要用戶發出請求,有request對象。工具
1.1 jsoup抓取ip信息測試
public static String getPublicIP() { String ip = ""; try { Document doc = Jsoup.connect("http://www.ip138.com/ip2city.asp").ignoreContentType(false).get(); Elements els = doc.select("center"); for (org.jsoup.nodes.Element el : els) { ip = el.text(); } ip = ip.replaceAll("[^0-9.]", ""); } catch (IOException e1) { e1.printStackTrace(); }finally { } return ip; }
1.2 request獲取ipui
public static String getIpAddr(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; }
本地啓動的服務返回的是127.0.0.1;url
內網訪問獲取的是內網IP地址;spa
遠程訪問獲取到公網IP。code
- 定義一個方法,將字符拼接成字符串
private static String readAll(Reader rd) throws IOException { StringBuilder sb = new StringBuilder(); int cp; while ((cp = rd.read()) != -1) { sb.append((char) cp); } return sb.toString(); }
- 將URL資源解析成json對象
public static JSONObject readJsonFromUrl(String url) throws IOException, JSONException { InputStream is = null; try { is = new URL(url).openStream(); BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8"))); String jsonText = readAll(rd); JSONObject json = JSONObject.fromObject(jsonText); // System.out.println(json); return json; } finally { //關閉輸入流 is.close(); } }
- 傳入用戶IP獲取當地地址名
public static String getAddrName(String IP) throws JSONException, IOException{ //這裏調用百度的ip定位api服務 詳見 http://api.map.baidu.com/lbsapi/cloud/ip-location-api.htm JSONObject json = readJsonFromUrl("http://api.map.baidu.com/location/ip?ak=iTrwV0ddxeFT6QUziPQh2wgGofxmWkmg&ip="+IP); /* 獲取到的json對象: * {"address":"CN|河北|保定|None|UNICOM|0|0", * "content":{"address_detail":{"province":"河北省","city":"保定市","street":"","district":"","street_number":"","city_code":307}, * "address":"河北省保定市","point":{"x":"12856963.35","y":"4678360.5"}}, * "status":0} */ //若是IP是本地127.0.0.1或者內網IP192.168則status分別返回1和2 String status = json.opt("status").toString(); if(!"0".equals(status)){ return ""; } JSONObject content=((JSONObject) json).getJSONObject("content"); //獲取json對象裏的content對象 JSONObject addr_detail=((JSONObject) content).getJSONObject("address_detail");//從content對象裏獲取address_detail String city=addr_detail.opt("city").toString(); //獲取市名,能夠根據具體需求更改 return city; }
測試一下,全部方法都是靜態的,類名getPuplic
//String scity=getPuplic.getAddrName("218.83.245.210"); //上海IP String IP1 = getPuplic.getPublicIP(); String IP2 = getPuplic.getIpAddr(request); String scity1=getPuplic.getAddrName(IP1); String scity2=getPuplic.getAddrName(IP2); logger.info("---------jsoup定位-------------"+scity1); logger.info("---------request-------------"+scity2);
用第一種方式須要引入的包
import org.jsoup.Connection; import org.jsoup.Jsoup; import org.jsoup.select.Elements; import net.sf.json.JSONException; import net.sf.json.JSONObject;
--------------------------------請多多指教