1 java代码对IP地址的合法性进行判断
java判断IP地址:
public static boolean ipIsValid(String ipSection, String ip) {
if (ipSection == null)
throw new NullPointerException("IP段不能为空!");
if (ip == null)
throw new NullPointerException("IP不能为空!");
ipSection = ipSection.trim();
ip = ip.trim();
final String REGX_IP = "((25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]\\d|\\d)\\.){3}(25[0-5]|2
[0-4]\\d|1\\d{2}|[1-9]\\d|\\d)";
final String REGX_IPB = REGX_IP + "\\-" + REGX_IP;
if (!ipSection.matches(REGX_IPB) || !ip.matches(REGX_IP))
return false;
int idx = ipSection.indexOf('-');
String[] sips = ipSection.substring(0, idx).split("\\.");
String[] sipe = ipSection.substring(idx + 1).split("\\.");
String[] sipt = ip.split("\\.");
long ips = 0L, ipe = 0L, ipt = 0L;
for (int i = 0; i < 4; ++i) {
ips = ips << 8 | Integer.parseInt(sips[i]);
ipe = ipe << 8 | Integer.parseInt(sipe[i]);
ipt = ipt << 8 | Integer.parseInt(sipt[i]);
}
if (ips > ipe) {
long t = ips;
ips = ipe;
ipe = t;
}
return ips <= ipt && ipt <= ipe;
}
2 判断一个字符是否是整数
public static booleab isInteger(String str){
Pattern pattern=Pattern.compile("^[-\\+]*$");
return pattern.matcher(str).matches();
}
3 判断一个字符是否是电话号码
public static boolean isPhoneNo(String str){
String regExp="^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$";
Pattern p=Pattern.compile(regExp);
Matcher m=p.matcher(str);
return m.matches();
}
4 为空判断
/**
* 检测字符串是否不为空(null,"","null")
*
* @param s
* @return 不为空则返回true,否则返回false
*/
public static boolean notEmpty(String s) {
return s != null && !"".equals(s) && !"null".equals(s);
}
5 获取MAC地址
/**
* 获得本机MAC地址
* @return 本机MAC地址
*/
public static String getLocalMacAddress() {
return getMacAddress(getLocalhost());
}
/**
* 获得指定地址信息中的MAC地址,使用分隔符“-”
* @param inetAddress {@link InetAddress}
* @return MAC地址,用-分隔
*/
public static String getMacAddress(InetAddress inetAddress) {
return getMacAddress(inetAddress, "-");
}
/**
* 获得指定地址信息中的MAC地址
* @param inetAddress {@link InetAddress}
* @param separator 分隔符,推荐使用“-”或者“:”
* @return MAC地址,用-分隔
*/
public static String getMacAddress(InetAddress inetAddress, String separator) {
if (null == inetAddress) {
return null;
}
byte[] mac=null;
try {
mac = NetworkInterface.getByInetAddress(inetAddress).getHardwareAddress();
} catch (SocketException e) {
e.printStackTrace();
}
if(null != mac){
final StringBuilder sb = new StringBuilder();
String s;
for (int i = 0; i < mac.length; i++) {
if (i != 0) {
sb.append(separator);
}
// 字节转换为整数
s = Integer.toHexString(mac[i] & 0xFF);
sb.append(s.length() == 1 ? 0 + s : s);
}
return sb.toString();
}
return null;
}
/**
* 获取本机网卡IP地址,这个地址为所有网卡中非回路地址的第一个<br>
* 如果获取失败调用 {@link InetAddress#getLocalHost()}方法获取。<br>
* 此方法不会抛出异常,获取失败将返回<code>null</code><br>
* @return 本机网卡IP地址,获取失败返回<code>null</code>
* @since 3.0.1
*/
public static InetAddress getLocalhost() {
InetAddress candidateAddress = null;
NetworkInterface iface;
InetAddress inetAddr;
try {
for (Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces(); ifaces.hasMoreElements();) {
iface = ifaces.nextElement();
for (Enumeration<InetAddress> inetAddrs = iface.getInetAddresses(); inetAddrs.hasMoreElements();) {
inetAddr = inetAddrs.nextElement();
if (false == inetAddr.isLoopbackAddress()) {
if (inetAddr.isSiteLocalAddress()) {
return inetAddr;
} else if (null == candidateAddress) {
// 非site-local地址做为候选地址返回
candidateAddress = inetAddr;
}
}
}
}
} catch (SocketException e) {
// ignore socket exception, and return null;
}
if (null == candidateAddress) {
try {
candidateAddress = InetAddress.getLocalHost();
} catch (UnknownHostException e) {
// ignore
}
}
return candidateAddress;
}