一站式
企业服务平台
400-6789-948

废话就不多说了,直接上码吧(废话不多说了直接上码吧)

废话就不多说了,直接上码吧!

IpUtils.java类

packagecom.fish.common.utils;importjava.net.InetAddress;importjava.net.UnknownHostException;importjavax.servlet.http.HttpServletRequest;publicclassIpUtils{publicstaticStringgetIpAddr(HttpServletRequestrequest){if(request==null){return"unknown";}Stringip=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("X-Forwarded-For");}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("X-Real-IP");}if(ip==null||ip.length()==0||"unknown".equalsIgnoreCase(ip)){ip=request.getRemoteAddr();}return"0:0:0:0:0:0:0:1".equals(ip)? "127.0.0.1":ip;}publicstaticbooleaninternalIp(Stringip){byte[]addr=textToNumericFormatV4(ip);returninternalIp(addr)||"127.0.0.1".equals(ip);}privatestaticbooleaninternalIp(byte[]addr){if(StringUtils.isNull(addr)||addr.length2){returntrue;}finalbyteb0=addr[0];finalbyteb1=addr[1];finalbyteSECTION_1=0x0A;finalbyteSECTION_2=(byte)0xAC;finalbyteSECTION_3=(byte)0x10;finalbyteSECTION_4=(byte)0x1F;finalbyteSECTION_5=(byte)0xC0;finalbyteSECTION_6=(byte)0xA8;switch(b0){caseSECTION_1:returntrue;caseSECTION_2:if(b1=SECTION_3&;&;b1=SECTION_4){returntrue;}caseSECTION_5:switch(b1){caseSECTION_6:returntrue;}default:returnfalse;}}publicstaticbyte[]textToNumericFormatV4(Stringtext){if(text.length()==0){returnnull;}byte[]bytes=newbyte[4];String[]elements=text.split(".",-1);try{longl;inti;switch(elements.length){case1:l=Long.parseLong(elements[0]);if((l0L)||(l4294967295L)){returnnull;}bytes[0]=(byte)(int)(l24&;0xFF);bytes[1]=(byte)(int)((l&;0xFFFFFF)16&;0xFF);bytes[2]=(byte)(int)((l&;0xFFFF)8&;0xFF);bytes[3]=(byte)(int)(l&;0xFF);break;case2:l=Integer.parseInt(elements[0]);if((l0L)||(l255L)){returnnull;}bytes[0]=(byte)(int)(l&;0xFF);l=Integer.parseInt(elements[1]);if((l0L)||(l16777215L)){returnnull;}bytes[1]=(byte)(int)(l16&;0xFF);bytes[2]=(byte)(int)((l&;0xFFFF)8&;0xFF);bytes[3]=(byte)(int)(l&;0xFF);break;case3:for(i=0;i2;++i){l=Integer.parseInt(elements[i]);if((l0L)||(l255L)){returnnull;}bytes[i]=(byte)(int)(l&;0xFF);}l=Integer.parseInt(elements[2]);if((l0L)||(l65535L)){returnnull;}bytes[2]=(byte)(int)(l8&;0xFF);bytes[3]=(byte)(int)(l&;0xFF);break;case4:for(i=0;i4;++i){l=Integer.parseInt(elements[i]);if((l0L)||(l255L)){returnnull;}bytes[i]=(byte)(int)(l&;0xFF);}break;default:returnnull;}}catch(NumberFormatExceptione){returnnull;}returnbytes;}publicstaticStringgetHostIp(){try{returnInetAddress.getLocalHost().getHostAddress();}catch(UnknownHostExceptione){}return"127.0.0.1";}publicstaticStringgetHostName(){try{returnInetAddress.getLocalHost().getHostName();}catch(UnknownHostExceptione){}return"未知";}}

获取地址类:AddressUtils.java

packagecom.fish.common.utils;importcom.fish.common.constant.Constants;importorg.slf4j.Logger;importorg.slf4j.LoggerFactory;importcom.alibaba.fastjson.JSONObject;importcom.fish.common.utils.http.HttpUtils;publicclassAddressUtils{privatestaticfinalLoggerlog=LoggerFactory.getLogger(AddressUtils.class);publicstaticfinalStringIP_URL="http://whois.pconline.com.cn/ipJson.jsp";publicstaticfinalStringUNKNOWN="XXXX";publicstaticStringgetRealAddressByIP(Stringip){Stringaddress=UNKNOWN;if(IpUtils.internalIp(ip)){return"内网IP";}try{StringrspStr=HttpUtils.sendGet(IP_URL,"ip="+ip+"&;json=true",Constants.GBK);if(StringUtils.isEmpty(rspStr)){log.error("获取地理位置异常{}",ip);returnUNKNOWN;}JSONObjectobj=JSONObject.parseObject(rspStr);Stringregion=obj.getString("pro");Stringcity=obj.getString("city");returnString.format("%s%s",region,city);}catch(Exceptione){log.error("获取地理位置异常{}",e);}returnaddress;}}

赞(8) 联系作者
未经允许不得转载:【聚禄鼎】一站式企业服务平台 » 废话就不多说了,直接上码吧(废话不多说了直接上码吧)
分享到

评论 抢沙发

让我们一起创建更加美好的网络世界

本站部分资源来自于网络收集,若侵犯了你的隐私或版权,请及时联系我们删除有关信息!

扫码添加站长微信

扫码添加小编微信

登录

找回密码

注册