java 获取定位_JAVA获取定位信息

这篇博客介绍了如何在JAVA中获取用户的地理位置信息,包括通过jsoup从网页抓取IP和从HttpServletRequest获取IP,并利用百度API将IP转换为具体的地址。
摘要由CSDN通过智能技术生成

通过用户IP获取用户地址信息

首先得到ip,有两种方式

第一种方式:通过jsoup从一些工具网页获取

第二种方式:通过获取用户对象信息得到

前者需要jsoup的jar包,不需要用户请求;

后者需要用户发出请求,有request对象。

1.1 jsoup抓取ip信息

public staticString 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{

}returnip;

}

1.2 request获取ip

public staticString 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();

}returnip;

}

本地启动的服务返回的是127.0.0.1;

内网访问获取的是内网IP地址;

远程访问获取到公网IP。

定义一个方法,将字符拼接成字符串

private static String readAll(Reader rd) throwsIOException {

StringBuilder sb= newStringBuilder();intcp;while ((cp = rd.read()) != -1) {

sb.append((char) cp);

}returnsb.toString();

}

将URL资源解析成json对象

public static JSONObject readJsonFromUrl(String url) throwsIOException, JSONException {

InputStream is= null;try{

is= newURL(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);

returnjson;

}finally{//关闭输入流

is.close();

}

}

传入用户IP获取当地地址名

public static String getAddrName(String IP) throwsJSONException, 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(); //获取市名,可以根据具体需求更改

returncity;

}

测试一下,所有方法都是静态的,类名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);

用第一种方式需要引入的包

importorg.jsoup.Connection;importorg.jsoup.Jsoup;importorg.jsoup.select.Elements;importnet.sf.json.JSONException;import net.sf.json.JSONObject;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值