|
|
@@ -4,6 +4,12 @@ import cn.iselab.mooctest.site.web.constants.AttrConsts;
|
|
|
|
|
|
import javax.servlet.http.Cookie;
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
+import java.io.IOException;
|
|
|
+import java.net.InetAddress;
|
|
|
+import java.net.NetworkInterface;
|
|
|
+import java.net.SocketException;
|
|
|
+import java.net.UnknownHostException;
|
|
|
+import java.util.Enumeration;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.Map;
|
|
|
|
|
|
@@ -64,4 +70,50 @@ public class RequestUtils {
|
|
|
}
|
|
|
return request.getParameterMap().get(name).length == 1 ? request.getParameterMap().get(name)[0] : "";
|
|
|
}
|
|
|
+
|
|
|
+ public static String getIP()throws IOException{
|
|
|
+ //String ip=System.getenv("HOST_IP");
|
|
|
+ System.out.println(System.getenv("M2_HOME"));
|
|
|
+ System.out.println(System.getenv("HOST_IP"));
|
|
|
+ Map<String,String> map=System.getenv();
|
|
|
+ String ip=map.get("HOST_IP");
|
|
|
+ System.out.println(ip);
|
|
|
+ if(ip==null){
|
|
|
+ ip = getLocalHostLANAddress().getHostAddress();
|
|
|
+ }
|
|
|
+ System.out.println(ip);
|
|
|
+ return "http://"+ip;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static InetAddress getLocalHostLANAddress() throws SocketException ,UnknownHostException{
|
|
|
+ try {
|
|
|
+ InetAddress candidateAddress = null;
|
|
|
+ // 遍历所有的网络接口
|
|
|
+ for (Enumeration ifaces = NetworkInterface.getNetworkInterfaces(); ifaces.hasMoreElements(); ) {
|
|
|
+ NetworkInterface iface = (NetworkInterface) ifaces.nextElement();
|
|
|
+ // 在所有的接口下再遍历IP
|
|
|
+ for (Enumeration inetAddrs = iface.getInetAddresses(); inetAddrs.hasMoreElements(); ) {
|
|
|
+ InetAddress inetAddr = (InetAddress) inetAddrs.nextElement();
|
|
|
+ if (!inetAddr.isLoopbackAddress()) {// 排除loopback类型地址
|
|
|
+ if (inetAddr.isSiteLocalAddress()) {
|
|
|
+ // 如果是site-local地址,就是它了
|
|
|
+ return inetAddr;
|
|
|
+ } else if (candidateAddress == null) {
|
|
|
+ // site-local类型的地址未被发现,先记录候选地址
|
|
|
+ candidateAddress = inetAddr;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (candidateAddress != null) {
|
|
|
+ return candidateAddress;
|
|
|
+ }
|
|
|
+ // 如果没有发现 non-loopback地址.只能用最次选的方案
|
|
|
+ InetAddress jdkSuppliedAddress = InetAddress.getLocalHost();
|
|
|
+ return jdkSuppliedAddress;
|
|
|
+ } catch (SocketException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
}
|