记录几个常用的java工具类
package cn.sh.ideal.web.util;
import cn.sh.ideal.system.entity.SystemConfig;
import cn.sh.ideal.system.service.ISystemConfigService;
import com.google.common.collect.Lists;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import javax.management.Query;
import java.lang.management.ManagementFactory;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.*;
/**
* 常用工具类
*/
@Component("webIpUtil")
public class WebIpUtil {
@Resource(name = "configService")
ISystemConfigService configService;
/**
* 判断当前服务器ip与配置ip是否一致
* 判断当前服务器文件路径是否包含配置文件名
*/
public String configCheckLocal() throws SocketException, UnknownHostException {
//服务器文件路径 和 服务器ip
String rootPath = getClass().getResource("/").getFile();
String serverIp = getLocalIP();
SystemConfig pathConfig = configService.findByKey("SERVER_ROOT_PATH");
JSONObject jsonObject = JSONObject.fromObject(pathConfig.getCfgValue());
for (Object key : jsonObject.keySet()) {
if (rootPath.contains(jsonObject.get(key).toString())) {
return serverIp + ":" + key;
}
}
return null;
}
/**
* 获取本地IP地址
*
* @throws SocketException
*/
public static String getLocalIP() throws UnknownHostException, SocketException {
if (isWindowsOS()) {
return InetAddress.getLocalHost().getHostAddress();
} else {
return getLinuxLocalIp();
}
}
/**
* 判断操作系统是否是Windows
*
* @return
*/
public static boolean isWindowsOS() {
boolean isWindowsOS = false;
String osName = System.getProperty("os.name");
if (osName.toLowerCase().indexOf("windows") > -1) {
isWindowsOS = true;
}
return isWindowsOS;
}
/**
* 获取Linux下的IP地址
*
* @return IP地址
* @throws SocketException
*/
private static String getLinuxLocalIp() throws SocketException {
String ip = "";
try {
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {
NetworkInterface intf = en.nextElement();
String name = intf.getName();
if (!name.contains("docker") && !name.contains("lo")) {
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress()) {
String ipaddress = inetAddress.getHostAddress().toString();
if (!ipaddress.contains("::") && !ipaddress.contains("0:0:") && !ipaddress.contains("fe80")) {
ip = ipaddress;
}
}
}
}
}
} catch (SocketException ex) {
ip = "127.0.0.1";
ex.printStackTrace();
}
return ip;
}
public static List<String> getEndPoints() {
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
Set<ObjectName> objs = null;
ArrayList<String> endPoints = null;
try {
objs = mbs.queryNames(new ObjectName("*:type=Connector,*"),
Query.match(Query.attr("protocol"), Query.value("HTTP/1.1")));
String hostname = InetAddress.getLocalHost().getHostName();
InetAddress[] addresses = InetAddress.getAllByName(hostname);
endPoints = Lists.newArrayList();
for (Iterator<ObjectName> i = objs.iterator(); i.hasNext(); ) {
ObjectName obj = i.next();
String port = obj.getKeyProperty("port");
endPoints.add(port);
}
} catch (Exception e) {
e.printStackTrace();
}
return endPoints;
}
}
测试工具类:
public static void main(String[] args) {
try {
String ip=WebIpUtil.getLocalIP();
Boolean isWindow=WebIpUtil.isWindowsOS();
String linuxIp=WebIpUtil.getLinuxLocalIp();
System.out.println("ip:"+ip+" isWindow:"+isWindow+" linuxIp:"+linuxIp);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (SocketException e) {
e.printStackTrace();
}
}
输出:ip:10.88.233.58 isWindow:true linuxIp:10.88.233.58