Commit 0e308a9b authored by 董建华's avatar 董建华

增加获取linux的IP的方法

parent a64fcc6e
......@@ -2,6 +2,7 @@ package com.controller;
import com.alibaba.fastjson.JSON;
import com.google.common.collect.ImmutableMap;
import com.util.IpUtil;
import jdk.nashorn.internal.ir.ReturnNode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
......@@ -12,6 +13,7 @@ import org.springframework.web.bind.annotation.RestController;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
......@@ -27,54 +29,54 @@ import java.util.concurrent.ConcurrentHashMap;
@RestController
@RequestMapping("/")
public class TestController {
private static Map<String,String> map = new ConcurrentHashMap<>();
private static Map<String, String> map = new ConcurrentHashMap<>();
@RequestMapping("/getAllIp")
public List<String> getAllIp() {
return IpUtil.getAllIP();
}
@RequestMapping("/getIp")
public String getIp(){
try {
String ip = InetAddress.getLocalHost().getHostAddress();
return ip;
} catch (UnknownHostException e) {
e.printStackTrace();
}
return null;
public String getIp() {
return IpUtil.getLocalIP();
}
@RequestMapping(value="check")
public ResponseEntity<String> check(){
if(map!=null && map.size()>0){
@RequestMapping(value = "check")
public ResponseEntity<String> check() {
if (map != null && map.size() > 0) {
return new ResponseEntity<String>(HttpStatus.NOT_FOUND);
}else{
} else {
return new ResponseEntity<String>(HttpStatus.OK);
}
}
@RequestMapping(value="/online")
public Map<String, String> online(){
@RequestMapping(value = "/online")
public Map<String, String> online() {
log.info("应用上线,流量开始切入....");
map.clear();
return responseOk();
}
@RequestMapping(value="/offline")
public Map<String, String> offline(){
@RequestMapping(value = "/offline")
public Map<String, String> offline() {
log.info("应用下线,流量不再切入....");
map.put("status", "offline");
return responseOk();
}
@RequestMapping(value="/test")
@RequestMapping(value = "/test")
public Map<String, String> test() {
log.info("CheckController测试!map = {}", map);
return ImmutableMap.of("code", "0", "msg", JSON.toJSONString(map));
}
public static Map<String, String> responseOk() {
return ImmutableMap.of("code", "0", "msg", "ok");
}
@RequestMapping("/getuuid")
public String getUuid(String param){
public String getUuid(String param) {
return UUID.nameUUIDFromBytes(param.getBytes()).toString();
}
......
package com.service;
package com.service.report;
import com.emums.BusinessType;
import com.emums.InfoType;
......
package com.service.impl;
package com.service.report.impl;
import com.config.ReportConfig;
import com.emums.BusinessType;
......@@ -7,7 +7,7 @@ import com.emums.PathType;
import com.entity.FileEntry;
import com.entity.report.ReportRecord;
import com.mapper.report.ReportRecordMapper;
import com.service.ReportService;
import com.service.report.ReportService;
import com.sun.jersey.api.client.ClientResponse;
import com.util.DateUtil;
import com.util.DingTalk;
......@@ -22,7 +22,6 @@ import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import java.io.*;
import java.nio.file.Path;
import java.util.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
......
package com.util;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
/**
* @author :dongjianhua
* @date :Created in 2019/11/1 15:42
* @description:获取本机IP的工具类
* @modified By:
* @version: 1.0
*/
public class IpUtil {
/**
* 02
* 获得主机IP
* 03
* <p>
* 04
*
* @return String
* 05
*/
public static boolean isWindowsOS() {
boolean isWindowsOS = false;
String osName = System.getProperty("os.name");
if (osName.toLowerCase().indexOf("windows") > -1) {
isWindowsOS = true;
}
return isWindowsOS;
}
public static List<String> getAllIP() {
List<String> list = new ArrayList<>();
try {
if (isWindowsOS()) {
InetAddress ip = InetAddress.getLocalHost();
list.add(ip.getHostAddress());
return list;
}
InetAddress ipAdd = null;
Enumeration<NetworkInterface> netInterfaces = (Enumeration<NetworkInterface>) NetworkInterface.getNetworkInterfaces();
while (netInterfaces.hasMoreElements()) {
NetworkInterface ni = (NetworkInterface) netInterfaces.nextElement();
//----------特定情况,可以考虑用ni.getName判断
//遍历所有ip
Enumeration<InetAddress> ips = ni.getInetAddresses();
while (ips.hasMoreElements()) {
ipAdd = (InetAddress) ips.nextElement();
list.add(ipAdd.getHostAddress());
}
}
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
/**
* 16
* 获取本机ip地址,并自动区分Windows还是linux操作系统
* 17
*
* @return String
* 18
*/
public static String getLocalIP() {
String sIP = "";
InetAddress ip = null;
try {
//如果是Windows操作系统
if (isWindowsOS()) {
ip = InetAddress.getLocalHost();
} else {//如果是Linux操作系统
boolean bFindIP = false;
Enumeration<NetworkInterface> netInterfaces = (Enumeration<NetworkInterface>) NetworkInterface.getNetworkInterfaces();
while (netInterfaces.hasMoreElements()) {
if (bFindIP) {
break;
}
NetworkInterface ni = (NetworkInterface) netInterfaces.nextElement();
//----------特定情况,可以考虑用ni.getName判断
//遍历所有ip
Enumeration<InetAddress> ips = ni.getInetAddresses();
while (ips.hasMoreElements()) {
ip = (InetAddress) ips.nextElement();
if (ip.isSiteLocalAddress() && !ip.isLoopbackAddress() //127.开头的都是lookback地址
&& ip.getHostAddress().indexOf(":") == -1) {
bFindIP = true;
break;
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
if (null != ip) {
sIP = ip.getHostAddress();
}
return sIP;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment