根据ip获取地址信息(得到返回的json对象里的值)

今天写登陆日志遇到了一个问题,就是根据获取到的ip获取地址(调用的百度api )

首先我写了两个工具类

1、这个是用来获取IP地址的(这个ip指的是网络ip,不是本地的ip地址)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import com.zzfeidu.bean.Admin;


@Controller
public class BaseController {
    //获取ip地址
    public static String getV4IP(){
        String ip = "";
        String chinaz = "http://ip.chinaz.com";

        StringBuilder inputLine = new StringBuilder();
        String read = "";
        URL url = null;
        HttpURLConnection urlConnection = null;
        BufferedReader in = null;
        try {
            url = new URL(chinaz);
            urlConnection = (HttpURLConnection) url.openConnection();
            in = new BufferedReader( new InputStreamReader(urlConnection.getInputStream(),"UTF-8"));
            while((read=in.readLine())!=null){
                inputLine.append(read+"\r\n");
            }
            //System.out.println(inputLine.toString());
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            if(in!=null){
                try {
                    in.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        Pattern p = Pattern.compile("\\
(.*?)\\<\\/dd>"); Matcher m = p.matcher(inputLine.toString()); if(m.find()){ String ipstr = m.group(1); ip = ipstr; //System.out.println(ipstr); } return ip; } }

2、

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.jdrupes.json.JsonObject;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sun.org.apache.bcel.internal.generic.IREM;

import net.sf.json.JSONArray;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;

public class AddressUtil {
     private static String readAll(Reader rd) throws IOException {
            StringBuilder sb = new StringBuilder();
            int cp;
            while ((cp = rd.read()) != -1) {
                sb.append((char) cp);
            }
            return sb.toString();
        }
        public static Object readJsonFrom(String ip) throws IOException, JSONException {
 InputStream is = new URL("http://api.map.baidu.com/location/ip?ak=XXXXXXXXXXXXXXXXXXXXXXXXX&ip="+ip).openStream();              
            try {
     BufferedReader rd = new BufferedReader(new InputStreamReader(is,Charset.forName("UTF-8")));
                String jsonText = readAll(rd);
                JSONObject  obj2 = new JSONObject().fromObject(jsonText);
                Object s=((JSONObject) obj2.get("content")).get("address");
                return s.toString();
            } finally {
                is.close();
            }
        }

}

结果如下:

obj2:{"address":"CN|河南|郑州|None|UNICOM|0|0","content":{"address_detail":{"province":"河南省","city":"郑州市","street":"","district":"","street_number":"","city_code":268},"address":"河南省郑州市","point":{"x":"12651558.14","y":"4106269.36"}},"status":0}
s:河南省郑州市

根据ip获取地址信息(得到返回的json对象里的值)_第1张图片
这是调用接口后返回的数据
不难看出我们调用接口返回的是一个json格式的数据,我们要想拿到里边的address的值,就需要用get去点了。
另外ak需要自己去百度申请的(免费的)可以参考一下这个地址:http://lbsyun.baidu.com/index.php?title=webapi/ip-api

最后就是在你登录之前调用方法,把获取到的ip和获得的地址插入到日志表中就行了。

你可能感兴趣的:(根据ip获取地址信息(得到返回的json对象里的值))