获取本机外网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);
JSONObject jsonObj= JSON.parseObject(ObjJson);
toIp=jsonObj.getString("cip");
} catch (Exception e) {
toIp="";
}
return toIp;
}
获取服务器IP地址
public static String getIfIP() {
String toIp="";
try{
String pathName="baidu.com";
InetAddress address =InetAddress.getAllByName(pathName)[0];
toIp=address.getHostAddress();
}catch (Exception e){
toIp="";
}
return toIp;
}