http://guoyunsky.javaeye.com/blog/662960
这里有详细的代码

解决方案 »

  1.   

    楼主可以参考下:
    http://javagp.group.javaeye.com/group/topic/11866
    http://www.javaeye.com/topic/340548
    http://topic.csdn.net/t/20050504/15/3983137.html
      

  2.   

    我觉得也是数据的问题一条sql应该就能搞定了吧
      

  3.   

    不用自己实现 搜个webservice接口 什么都搞定
      

  4.   


    import java.util.regex.Matcher;
    import java.util.regex.Pattern;import org.apache.commons.httpclient.HttpClient;
    import org.apache.commons.httpclient.NameValuePair;
    import org.apache.commons.httpclient.methods.PostMethod;
    import org.apache.commons.httpclient.params.HttpMethodParams;
    /**
     * 通过手机号码,获得该号码的归属地
     * 
     * @author Administrator
     *
     */
    public class MobileFromUtil {
    //正则表达式,抽取手机归属地
    public static final String REGEX_GET_MOBILE=
    "(?is)(<tr[^>]+>[\\s]*<td[^>]+>[\\s]*卡号归属地[\\s]*</td>[\\s]*<td[^>]+>([^<]+)</td>[\\s]*</tr>)"; //2:from
    //正则表达式,审核要获取手机归属地的手机是否符合格式,可以只输入手机号码前7位
    public static final String REGEX_IS_MOBILE=
    "(?is)(^1[3|4|5|8][0-9]\\d{4,8}$)";

    /**
     * 获得手机号码归属地
     * 
     * @param mobileNumber
     * @return
     * @throws Exception
     */
    public static String getMobileFrom(String mobileNumber) throws Exception {
    if(!veriyMobile(mobileNumber)){
    throw new Exception("不是完整的11位手机号或者正确的手机号前七位");
    }
    HttpClient client=null;
    PostMethod method=null;
    NameValuePair mobileParameter=null;
    NameValuePair actionParameter=null;
    int httpStatusCode;

    String htmlSource=null;
    String result=null;


    try {
    client=new HttpClient();
    client.getHostConfiguration().setHost("www.ip138.com", 8080, "http");
    method=new PostMethod("/search.asp");
    mobileParameter=new NameValuePair("mobile",mobileNumber);
    actionParameter=new NameValuePair("action","mobile");
    method.setRequestBody(new NameValuePair[] { actionParameter,mobileParameter }); 
    //设置编码
    method.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "GB2312");

    client.executeMethod(method);
    httpStatusCode=method.getStatusLine().getStatusCode();
    if(httpStatusCode!=200){
    throw new Exception("网页内容获取异常!Http Status Code:"+httpStatusCode);
    }

    htmlSource=method.getResponseBodyAsString();
    if(htmlSource!=null&&!htmlSource.equals("")){
    result=parseMobileFrom(htmlSource);
    }
    } catch (RuntimeException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }finally{
    method.releaseConnection();
    }

    return result;

    }
    /**
     * 从www.ip138.com返回的结果网页内容中获取手机号码归属地,结果为:省份 城市
     * 
     * @param htmlSource
     * @return
     */
    public static String parseMobileFrom(String htmlSource){
    Pattern p=null;
    Matcher m=null;
    String result=null;

    p=Pattern.compile(REGEX_GET_MOBILE);
    m=p.matcher(htmlSource);

    while(m.find()){
    if(m.start(2)>0){
    result=m.group(2);
    result=result.replaceAll("&nbsp;", " ");
    }
    }


    return result;

    }

    /**
     * 验证手机号
     * @param mobileNumber
     * @return
     */
    public static boolean veriyMobile(String mobileNumber){
    Pattern p=null;
    Matcher m=null;

    p=Pattern.compile(REGEX_IS_MOBILE);
    m=p.matcher(mobileNumber);

    return m.matches();
    }

    /**
     * 测试
     * @param args
     * @throws Exception 
     */
    public static void main(String[] args) throws Exception {
    System.out.println(getMobileFrom("13888888888"));
    }}
      

  5.   

    这个在网上找个WebService接口开发一下,应该不难
      

  6.   


    这个代码为何如此的纠结呢 我直接运行的时候 每次测试 都是返回null当时当我在 
               if(httpStatusCode!=200){
                    throw new Exception("网页内容获取异常!Http Status Code:"+httpStatusCode);
                }
                
    后 输出了一下 httpStatusCode 就运行正常 - -