Эх сурвалжийг харах

解决nginx代理后,获取的地址为前端地址问题。

Gaokun Wang 4 сар өмнө
parent
commit
efd954936d

+ 36 - 4
als-modules/system/src/main/java/org/eco/system/service/impl/CommonService.java

@@ -1,24 +1,31 @@
 package org.eco.system.service.impl;
 
-import org.eco.common.core.annotation.Excel;
+import lombok.extern.slf4j.Slf4j;
 import org.eco.common.core.config.EcoConfig;
 import org.eco.common.core.exception.BusinessException;
 import org.eco.common.core.utils.StringUtils;
 import org.eco.common.core.utils.file.FileUploadUtils;
 import org.eco.system.config.ServerConfig;
-import org.eco.system.domain.vo.SysOssUploadVo;
-import org.eco.system.domain.vo.SysOssVo;
 import org.eco.system.service.ICommonService;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
 import org.springframework.web.multipart.MultipartFile;
 
+import java.net.Inet4Address;
+import java.net.InetAddress;
+import java.net.NetworkInterface;
+import java.util.Enumeration;
 import java.util.HashMap;
 import java.util.Map;
 
+@Slf4j
 @Service
 public class CommonService implements ICommonService {
 
+    @Value("${server.port:8080}")
+    private Long port;
+
     private static final String FILE_DELIMETER = ",";
     @Autowired
     private ServerConfig serverConfig;
@@ -35,7 +42,7 @@ public class CommonService implements ICommonService {
             String originalFilename = file.getOriginalFilename();
             assert originalFilename != null;
             String suffix = StringUtils.substring(originalFilename, originalFilename.lastIndexOf("."), originalFilename.length());
-            Map<String,String> map = new HashMap<>();
+            Map<String, String> map = new HashMap<>();
             map.put("url", url);
             map.put("originalFilename", originalFilename);
             map.put("suffix", suffix);
@@ -45,4 +52,29 @@ public class CommonService implements ICommonService {
             throw new BusinessException(e.getMessage());
         }
     }
+
+    public static String getIpAddress() {
+        try {
+            //从网卡中获取IP
+            Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface.getNetworkInterfaces();
+            InetAddress ip;
+            while (allNetInterfaces.hasMoreElements()) {
+                NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
+                //用于排除回送接口,非虚拟网卡,未在使用中的网络接口
+                if (!netInterface.isLoopback() && !netInterface.isVirtual() && netInterface.isUp()) {
+                    //返回和网络接口绑定的所有IP地址
+                    Enumeration<InetAddress> addresses = netInterface.getInetAddresses();
+                    while (addresses.hasMoreElements()) {
+                        ip = addresses.nextElement();
+                        if (ip instanceof Inet4Address) {
+                            return ip.getHostAddress();
+                        }
+                    }
+                }
+            }
+        } catch (Exception e) {
+            log.info("IP地址获取失败" + e.getMessage());
+        }
+        return "127.0.0.1";
+    }
 }