MachineIp.java 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package com.yc.education.util;
  2. import java.io.IOException;
  3. import java.net.Inet4Address;
  4. import java.net.InetAddress;
  5. import java.net.InterfaceAddress;
  6. import java.net.NetworkInterface;
  7. import java.net.SocketException;
  8. import java.util.Enumeration;
  9. import java.util.List;
  10. public class MachineIp {
  11. /**
  12. * 获取本机Ip
  13. *
  14. * 通过 获取系统所有的networkInterface网络接口 然后遍历 每个网络下的InterfaceAddress组。
  15. * 获得符合 <code>InetAddress instanceof Inet4Address</code> 条件的一个IpV4地址
  16. * @return
  17. */
  18. @SuppressWarnings("rawtypes")
  19. public static String localIp() {
  20. String ip = null;
  21. Enumeration allNetInterfaces;
  22. try {
  23. allNetInterfaces = NetworkInterface.getNetworkInterfaces();
  24. while (allNetInterfaces.hasMoreElements()) {
  25. NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
  26. List<InterfaceAddress> InterfaceAddress = netInterface.getInterfaceAddresses();
  27. for (InterfaceAddress add : InterfaceAddress) {
  28. InetAddress Ip = add.getAddress();
  29. if (Ip != null && Ip instanceof Inet4Address) {
  30. ip = Ip.getHostAddress();
  31. }
  32. }
  33. }
  34. } catch (SocketException e) {
  35. e.printStackTrace();
  36. //logger.warn("获取本机Ip失败:异常信息:"+e.getMessage());
  37. }
  38. return ip;
  39. }
  40. public static void main(String[] args) throws IOException {
  41. /*System.err.println(localIp());*/
  42. SM4 sm4 = new SM4( );
  43. String decode = sm4.decode("pVUnxpFL39NMFFupXDwBdA==", "W5#");
  44. System.out.println(decode );
  45. }
  46. }