Java通过IP获取所在地
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                Java通过IP获取所在地
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                Java通過IP獲取所在地
我在這用的是騰訊地圖api
?
/** * * @param content * 請求的參數 格式為:name=xxx&pwd=xxx * @param encoding * 服務器端請求編碼。如GBK,UTF-8等 * @return * @throws UnsupportedEncodingException */ public static String getAddresses(String content, String encodingString) throws UnsupportedEncodingException { String urlStr = "https://apis.map.qq.com/ws/location/v1/ip"; String returnStr = getResult(urlStr, content, encodingString); if (returnStr != null) { // 處理返回的省市區信息 //System.out.println("(1) unicode轉換成中文前的returnStr : " + returnStr); //returnStr = decodeUnicode(returnStr); //System.out.println("(2) unicode轉換成中文后的returnStr : " + returnStr); String[] temp = returnStr.split(","); if(temp.length<3){ return "0";//無效IP,局域網測試 } return returnStr; } return null; } /** * @param urlStr * 請求的地址 * @param content * 請求的參數 格式為:name=xxx&pwd=xxx * @param encoding * 服務器端請求編碼。如GBK,UTF-8等 * @return */ private static String getResult(String urlStr, String content, String encoding) { URL url = null; HttpURLConnection connection = null; try { url = new URL(urlStr+content); connection = (HttpURLConnection) url.openConnection();// 新建連接實例 //connection.setConnectTimeout(2000);// 設置連接超時時間,單位毫秒 //connection.setReadTimeout(2000);// 設置讀取數據超時時間,單位毫秒 //connection.setDoOutput(true);// 是否打開輸出流 true|false //connection.setDoInput(true);// 是否打開輸入流true|false //connection.setRequestMethod("POST");// 提交方法POST|GET //connection.setUseCaches(false);// 是否緩存true|false //connection.connect();// 打開連接端口 //DataOutputStream out = new DataOutputStream(connection // .getOutputStream());// 打開輸出流往對端服務器寫數據 //out.writeBytes(content);// 寫數據,也就是提交你的表單 name=xxx&pwd=xxx //out.flush();// 刷新 //out.close();// 關閉輸出流 BufferedReader reader = new BufferedReader(new InputStreamReader( connection.getInputStream(), encoding));// 往對端寫完數據對端服務器返回數據 // ,以BufferedReader流來讀取 StringBuffer buffer = new StringBuffer(); String line = ""; while ((line = reader.readLine()) != null) { buffer.append(line); }reader.close(); return buffer.toString(); } catch (IOException e) { e.printStackTrace(); } finally { if (connection != null) { connection.disconnect();// 關閉連接 } } return null; }?
?填寫要查詢的ip key填寫騰訊地圖服務平臺-->控制臺你的key
public static void main(String[] args) {填寫要查詢的ip key填寫騰訊地圖服務平臺-->控制臺你的keyString ip =""; String key = "";String address = ""; try { address = Gituser.getAddresses("?ip="+ip+"&key="+key, "utf-8"); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } //去除返回數據的空格和{ }符號String add = address.replace(" ", "").replace("{", "").replace("}", "");//遍歷輸出String[] arr = add.split(",");for(int i =0;i<arr.length;i++){System.out.println(arr[i]);} }?
總結
以上是生活随笔為你收集整理的Java通过IP获取所在地的全部內容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: flume记录(2)监控文件和目录,对h
 - 下一篇: 计算机无法安装hp网络打印机,安装HP网