IPUtil工具类,java 获取IP所属地


package com.util.net;


import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;


import javax.servlet.http.HttpServletRequest;


import org.apache.log4j.Logger;


import net.sf.json.JSONObject;


/**
 * @ClassName: IPUtil
 * @version 1.0 
 * @Desc: Ip工具类
 * @author huaping hu
 * @date 2016年6月1日下午5:26:56
 * @history v1.0
 *
 */
public class IPUtil {


public final static Logger LOGGER = Logger.getLogger(IPUtil.class);
/**

* 描述:获取IP地址
* @author huaping hu 
* @date 2016年6月1日下午5:25:44
* @param request
* @return
*/
public static String getIpAddress(HttpServletRequest request){

String ip = request.getHeader("x-forwarded-for");

if(ip == null || ip.length() ==0 || "nuknown".equalsIgnoreCase(ip)){

ip = request.getHeader("Proxy-Client-IP");
}
if(ip == null || ip.length() ==0 || "nuknown".equalsIgnoreCase(ip)){

ip = request.getHeader("WL-Proxy-Client-IP");
}
if(ip == null || ip.length() ==0 || "nuknown".equalsIgnoreCase(ip)){

ip = request.getRemoteAddr();
}
return ip;
}


/**

* 描述:获取IP+[IP所属地址]
* @author huaping hu 
* @date 2016年6月1日下午6:01:09
* @param request
* @return
*/
public static String getIpBelongAddress(HttpServletRequest request){

String ip = getIpAddress(request);
String belongIp = getIPbelongAddress(ip);

return ip + belongIp;
}
/**

* 描述:获取IP所属地址
* @author huaping hu 
* @date 2016年6月1日下午5:59:43
* @param ip
* @return
*/
public static String getIPbelongAddress(String ip){

String ipAddress = "[]";
try{
//淘宝提供的服务地址
String context = call("http://ip.taobao.com/service/getIpInfo.php?ip="+ip);
JSONObject fromObject = JSONObject.fromObject(context);
String code = fromObject.getString("code");
if(code.equals("0")){
JSONObject jsonObject = fromObject.getJSONObject("data");
ipAddress =  "["+jsonObject.get("country")+"/" +jsonObject.get("city")+"]";
        }
        }catch(Exception e){
             
        LOGGER.error("获取IP所属地址出错",e);
            e.printStackTrace();
        }
        return ipAddress;
}
/**

* 描述:获取Ip所属地址
* @author huaping hu 
* @date 2016年6月1日下午5:38:55
* @param urlStr
* @return
*/
public static String call( String urlStr ){

try {

URL url = new URL(urlStr);
HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();

httpCon.setConnectTimeout(3000);
httpCon.setDoInput(true);
httpCon.setRequestMethod("GET");

int code = httpCon.getResponseCode();

if(code == 200){
return streamConvertToSting(httpCon.getInputStream());
}
} catch (Exception e) {
LOGGER.error("获取IP所属地址出错",e);
e.printStackTrace();
}
return null;
}

/**

* 描述:将InputStream转换成String
* @author huaping hu 
* @date 2016年6月1日下午5:51:53
* @param is
* @return
*/
public static String streamConvertToSting(InputStream is){

String tempStr = "";
try {

if(is == null) return null;
ByteArrayOutputStream arrayOut = new ByteArrayOutputStream();
byte[] by = new byte[1024];
int len = 0;
while((len = is.read(by))!=-1){
arrayOut.write(by,0,len);
}
tempStr = new String(arrayOut.toByteArray());

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return tempStr;
}

public static void main(String[] args) {


String context = call("http://ip.taobao.com/service/getIpInfo.php?ip=120.192.182.1");

JSONObject fromObject = JSONObject.fromObject(context);
JSONObject jsonObject = fromObject.getJSONObject("data");
System.out.println(fromObject);
System.err.println(jsonObject.get("city"));
}
}
  • 5
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值