以下内容介绍下java获取ip地址的几种思路. 1.直接利用java.net.InetAddress类获取,不过这种方法只在windows环境下有效,在linux环境下只能获取localhost地址(即/etc/hosts文件内容) 代码如下: import java.net.InetAddress; /** * This method works well in windows system. * In Linux system it returns 127.0.0.1 the content…
下面代码是关于Java将ip字符串转换成整数的代码,希望对各位有较大用途. public class IpUtil { public static int Ip2Int(String strIp){ String[] ss = strIp.split("\."); if(ss.length != 4){ return 0; } byte[] bytes = new byte[ss.length]; for(int i = 0; i < bytes.length; i++){ by…
Java程序IP v6与IP v4的设置 //Prevent to get IPV6 address,this way only work in debug mode //But you can pass use -Djava.net.preferIPv4Stack=true,then it work well whether in debug mode or not System.setProperty("java.net.preferIPv4Stack", "true&q…