java获取本地外网ip地址
生活随笔
收集整理的這篇文章主要介紹了
java获取本地外网ip地址
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
獲取本機(jī)外網(wǎng)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;}獲取服務(wù)器IP地址
/*** 根據(jù)域名獲取ip地址*/public static String getIfIP() {String toIp="";try{String pathName="baidu.com";//這里沒(méi)有進(jìn)行支持https協(xié)議的,可以自行進(jìn)行切割字符串InetAddress address =InetAddress.getAllByName(pathName)[0];toIp=address.getHostAddress();}catch (Exception e){toIp="";}return toIp;}總結(jié)
以上是生活随笔為你收集整理的java获取本地外网ip地址的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: ECharts的点击事件
- 下一篇: java加密字符串,可解密