Java查询IP地址所在地

[java]  view plain  copy
  1. package ip;  
  2.    
  3. import java.io.ByteArrayOutputStream;  
  4. import java.io.IOException;  
  5. import java.io.InputStream;  
  6. import java.net.HttpURLConnection;  
  7. import java.net.MalformedURLException;  
  8. import java.net.URL;  
  9.    
  10. import net.sf.json.JSONObject;  
  11.    
  12. public class GetAddressByIp  
  13. {  
  14.        
  15.      public static void main(String[] args) {  
  16.          String getAddressByIp = GetAddressByIp("59.60.9.2");  
  17.          System.out.println(getAddressByIp);  
  18.     }  
  19.     /** 
  20.      *  
  21.      * @param IP 
  22.      * @return 
  23.      */  
  24.     public static String GetAddressByIp(String IP){  
  25.         String resout = "";  
  26.         try{  
  27.          String str = getJsonContent("http://ip.taobao.com/service/getIpInfo.php?ip="+IP);  
  28.            
  29.          System.out.println(str);  
  30.             
  31.          JSONObject obj = JSONObject.fromObject(str);  
  32.          JSONObject obj2 =  (JSONObject) obj.get("data");  
  33.          String code = String.valueOf(obj.get("code"));  
  34.          if(code.equals("0")){  
  35.             
  36.              resout =  obj2.get("country")+"--" +obj2.get("area")+"--" +obj2.get("city")+"--" +obj2.get("isp");  
  37.          }else{  
  38.              resout =  "IP地址有误";  
  39.          }  
  40.         }catch(Exception e){  
  41.                
  42.             e.printStackTrace();  
  43.              resout = "获取IP地址异常:"+e.getMessage();  
  44.         }  
  45.         return resout;  
  46.             
  47.     }  
  48.       
  49.     public static String getJsonContent(String urlStr)  
  50.     {  
  51.         try  
  52.         {// 获取HttpURLConnection连接对象  
  53.             URL url = new URL(urlStr);  
  54.             HttpURLConnection httpConn = (HttpURLConnection) url  
  55.                     .openConnection();  
  56.             // 设置连接属性  
  57.             httpConn.setConnectTimeout(3000);  
  58.             httpConn.setDoInput(true);  
  59.             httpConn.setRequestMethod("GET");  
  60.             // 获取相应码  
  61.             int respCode = httpConn.getResponseCode();  
  62.             if (respCode == 200)  
  63.             {  
  64.                 return ConvertStream2Json(httpConn.getInputStream());  
  65.             }  
  66.         }  
  67.         catch (MalformedURLException e)  
  68.         {  
  69.             e.printStackTrace();  
  70.         }  
  71.         catch (IOException e)  
  72.         {  
  73.             e.printStackTrace();  
  74.         }  
  75.         return "";  
  76.     }  
  77.    
  78.       
  79.     private static String ConvertStream2Json(InputStream inputStream)  
  80.     {  
  81.         String jsonStr = "";  
  82.         // ByteArrayOutputStream相当于内存输出流  
  83.         ByteArrayOutputStream out = new ByteArrayOutputStream();  
  84.         byte[] buffer = new byte[1024];  
  85.         int len = 0;  
  86.         // 将输入流转移到内存输出流中  
  87.         try  
  88.         {  
  89.             while ((len = inputStream.read(buffer, 0, buffer.length)) != -1)  
  90.             {  
  91.                 out.write(buffer, 0, len);  
  92.             }  
  93.             // 将内存流转换为字符串  
  94.             jsonStr = new String(out.toByteArray());  
  95.         }  
  96.         catch (IOException e)  
  97.         {  
  98.             // TODO Auto-generated catch block  
  99.             e.printStackTrace();  
  100.         }  
  101.         return jsonStr;  
  102.     }  
  103. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值