`
shine1200
  • 浏览: 40240 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

一段获取ip所在地的java代码

 
阅读更多
需要除jdk自带的包以外的
commons-logging-1.1.1.jar
commons-codec-1.4.jar
httpclient-4.1.1.jar
httpclient-cache-4.1.1.jar
httpcore-4.1.jar
httpmime-4.1.1.jar
必须的哦,一个都不能少

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;



public class ipUtil {

  
     * @param args
     * @author luChangHua
     * @throws IOException
     * @throws ClientProtocolException
     * @createDate 2011-7-4上午10:27:27
   
    public static void main(String[] args) throws ClientProtocolException, IOException {
      
        Map map = getIpMessage("125.123.70.73");
      
        System.out.println(map.get("ip"));
      
        System.out.println(map.get("address"));
    }
   
  
     * 获得ip的所在地
     * @param ip
     * @return map{"ip":ip,"address",address}
     * @throws ClientProtocolException
     * @throws IOException
     * @author luChangHua
     * @createDate 2011-7-4下午03:36:10
   
    public static HashMap  getIpMessage(String ip) throws ClientProtocolException, IOException{
        HashMap map = new HashMap();
      
        String result= "";
      
        String url = "http://www.youdao.com/smartresult-xml/search.s?type=ip&q="+ip;
      
        result = httPGet(url);
      
        String thisIp = findByReg("<ip>\\s*?([^<>]*?)\\s*?</ip>",result);
      
        String location = findByReg("\\s*?<location>(.*?)</location",result);
      
        return map;
    }
  
     * 用正则表达式匹配内容
     * @param reg 正则表达式
     * @param content 进行匹配的内容
     * @return 匹配到的内容
     * @author luChangHua
     * @createDate 2011-7-4下午03:37:49
   
    public static String findByReg(String reg,String content){
      
        String result = "";
      
        Pattern pattern =Pattern.compile(reg);
      
        Matcher matcher = pattern.matcher(content);
      
        if(matcher.find()){
          
            result = matcher.group(1);
          
        }
      
        return result;
    }
   
     *
     * 简易的httpclient get方法
     * @param url 需要请求的链接
     * @return    打开链接的内容
     * @throws ClientProtocolException
     * @throws IOException
     * @author luChangHua
     * @createDate 2011-7-4下午03:39:18
     *
    public static String httPGet(String url) throws ClientProtocolException, IOException{
      
        String result= "";
      
        HttpClient httpclient = new DefaultHttpClient();

      
        HttpGet httpGet = new HttpGet(url);
      
        HttpResponse response =  httpclient.execute(httpGet);
      
        HttpEntity  entity = response.getEntity();
      
        result = EntityUtils.toString(entity);      

        httpclient.getConnectionManager().shutdown();
      
        return result ;
      
    }
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics