java获取本地外网ip地址
生活随笔
收集整理的這篇文章主要介紹了
java获取本地外网ip地址
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
獲取本機外網ip地址
public static String getIP() {String ip = "http://pv.sohu.com/cityjson?ie=utf-8";String inputLine = "";String read = "";String toIp="";try {URL url = new URL(ip);HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));while ((read = in.readLine()) != null) {inputLine += read;}String ObjJson=inputLine.substring(inputLine.indexOf("=")+1,inputLine.length()-1); // System.out.println(ObjJson);JSONObject jsonObj= JSON.parseObject(ObjJson);toIp=jsonObj.getString("cip"); // throw new Exception();} catch (Exception e) {toIp="";}return toIp;}獲取服務器IP地址
/*** 根據域名獲取ip地址*/public static String getIfIP() {String toIp="";try{String pathName="baidu.com";//這里沒有進行支持https協議的,可以自行進行切割字符串InetAddress address =InetAddress.getAllByName(pathName)[0];toIp=address.getHostAddress();}catch (Exception e){toIp="";}return toIp;}總結
以上是生活随笔為你收集整理的java获取本地外网ip地址的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ECharts的点击事件
- 下一篇: 小米汽车要来了?传已经小批量试生产 或明