Commit e74ceb27a87c87e97bb6f8d129727ca1ab8b536e

Authored by 王通
1 parent a4ff8a76

1.获取本地ip地址

src/main/java/com/bsth/service/impl/TrafficManageServiceImpl.java
@@ -19,6 +19,7 @@ import com.bsth.repository.traffic.SKBUploadLoggerRepository; @@ -19,6 +19,7 @@ import com.bsth.repository.traffic.SKBUploadLoggerRepository;
19 import com.bsth.security.util.SecurityUtils; 19 import com.bsth.security.util.SecurityUtils;
20 import com.bsth.service.TrafficManageService; 20 import com.bsth.service.TrafficManageService;
21 import com.bsth.service.traffic.YgcBasicDataService; 21 import com.bsth.service.traffic.YgcBasicDataService;
  22 +import com.bsth.util.IpUtils;
22 import com.bsth.util.TimeUtils; 23 import com.bsth.util.TimeUtils;
23 import com.bsth.util.db.DBUtils_MS; 24 import com.bsth.util.db.DBUtils_MS;
24 import com.bsth.webService.trafficManage.org.tempuri.Results; 25 import com.bsth.webService.trafficManage.org.tempuri.Results;
@@ -500,7 +501,7 @@ public class TrafficManageServiceImpl implements TrafficManageService{ @@ -500,7 +501,7 @@ public class TrafficManageServiceImpl implements TrafficManageService{
500 try { 501 try {
501 //发送邮件 502 //发送邮件
502 EmailBean mail = new EmailBean(); 503 EmailBean mail = new EmailBean();
503 - mail.setSubject(InetAddress.getLocalHost().getHostAddress()+":路单日志数据"+date); 504 + mail.setSubject(IpUtils.getLocalIpAddress() +":路单日志数据"+date);
504 mail.setContent("总数:" + (listGroup == null ? 0 : listGroup.size()) + "<br/>成功数:" + scount + "<br/>跳过数:" + ccount + "<br/>耗时:" + (System.currentTimeMillis() - start)); 505 mail.setContent("总数:" + (listGroup == null ? 0 : listGroup.size()) + "<br/>成功数:" + scount + "<br/>跳过数:" + ccount + "<br/>耗时:" + (System.currentTimeMillis() - start));
505 sendEmailController.sendMail(emailSendToAddress, mail); 506 sendEmailController.sendMail(emailSendToAddress, mail);
506 logger.info("setLD-sendMail:邮件发送成功!"); 507 logger.info("setLD-sendMail:邮件发送成功!");
src/main/java/com/bsth/util/IpUtils.java
1 -package com.bsth.util;  
2 -  
3 -import javax.servlet.http.HttpServletRequest;  
4 -  
5 -public class IpUtils {  
6 -  
7 - public static String getIpAddr(HttpServletRequest request) {  
8 - if (request == null) {  
9 - return "unknown";  
10 - }  
11 - String ip = request.getHeader("x-forwarded-for");  
12 - if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
13 - ip = request.getHeader("Proxy-Client-IP");  
14 - }  
15 - if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
16 - ip = request.getHeader("X-Forwarded-For");  
17 - }  
18 - if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
19 - ip = request.getHeader("WL-Proxy-Client-IP");  
20 - }  
21 - if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
22 - ip = request.getHeader("X-Real-IP");  
23 - }  
24 - if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
25 - ip = request.getRemoteAddr();  
26 - }  
27 - return ip;  
28 - }  
29 -} 1 +package com.bsth.util;
  2 +
  3 +import javax.servlet.http.HttpServletRequest;
  4 +import java.net.Inet4Address;
  5 +import java.net.InetAddress;
  6 +import java.net.NetworkInterface;
  7 +import java.util.Enumeration;
  8 +
  9 +public class IpUtils {
  10 +
  11 + public static String getIpAddr(HttpServletRequest request) {
  12 + if (request == null) {
  13 + return "unknown";
  14 + }
  15 + String ip = request.getHeader("x-forwarded-for");
  16 + if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
  17 + ip = request.getHeader("Proxy-Client-IP");
  18 + }
  19 + if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
  20 + ip = request.getHeader("X-Forwarded-For");
  21 + }
  22 + if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
  23 + ip = request.getHeader("WL-Proxy-Client-IP");
  24 + }
  25 + if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
  26 + ip = request.getHeader("X-Real-IP");
  27 + }
  28 + if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
  29 + ip = request.getRemoteAddr();
  30 + }
  31 + return ip;
  32 + }
  33 +
  34 + /**
  35 + * 获取本地IP地址
  36 + * @return
  37 + */
  38 + public static String getLocalIpAddress() {
  39 + try {
  40 + Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface.getNetworkInterfaces();
  41 + InetAddress ip = null;
  42 + while (allNetInterfaces.hasMoreElements()) {
  43 + NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
  44 + if (netInterface.isLoopback() || netInterface.isVirtual() || !netInterface.isUp() || "docker0".equals(netInterface.getDisplayName())) {
  45 + continue;
  46 + } else {
  47 + Enumeration<InetAddress> addresses = netInterface.getInetAddresses();
  48 + while (addresses.hasMoreElements()) {
  49 + ip = addresses.nextElement();
  50 + if (ip != null && ip instanceof Inet4Address) {
  51 + return ip.getHostAddress();
  52 + }
  53 + }
  54 + }
  55 + }
  56 + } catch (Exception e) {
  57 + System.err.println("IP地址获取失败" + e.toString());
  58 + }
  59 +
  60 + return "";
  61 + }
  62 +}