NetUtils.java
1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package com.bs.fileservice.utils;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class NetUtils {
public static Long getLocalNodeId() {
InetAddress address = localInetAddress();
if (address == null) {
return null;
} else {
byte[] ipAddressByteArray = address.getAddress();
long node1 = (long) ((ipAddressByteArray[ipAddressByteArray.length - 2] & 3) << 8);
long node2 = (long) (ipAddressByteArray[ipAddressByteArray.length - 1] & 255);
Long nodeValue = node1 + node2;
return nodeValue;
}
}
private static InetAddress localInetAddress() {
InetAddress address = null;
try {
address = InetAddress.getByName(getSelfIP());
return address;
} catch (UnknownHostException var2) {
throw new IllegalStateException("Cannot get LocalHost InetAddress, please check your network!", var2);
}
}
public static String getSelfIP() {
InetAddress inetAddress = null;
try {
inetAddress = InetAddress.getLocalHost();
} catch (UnknownHostException var3) {
var3.printStackTrace();
}
String tmpSelfIP = inetAddress.getHostName();
String tmpSelfIP2 = inetAddress.getCanonicalHostName();
System.out.println("IP:" + tmpSelfIP);
System.out.println("IPTO:" + tmpSelfIP2);
return tmpSelfIP;
}
}