|
@@ -0,0 +1,99 @@
|
|
|
+package cn.kdan.pdf.tech.core.utils;
|
|
|
+
|
|
|
+import java.io.*;
|
|
|
+import java.net.InetAddress;
|
|
|
+import java.net.NetworkInterface;
|
|
|
+import java.util.Enumeration;
|
|
|
+
|
|
|
+public class ServerUtils {
|
|
|
+ public static String getRealDeviceId() {
|
|
|
+ try {
|
|
|
+ Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
|
|
|
+ while (interfaces.hasMoreElements()) {
|
|
|
+ NetworkInterface networkInterface = interfaces.nextElement();
|
|
|
+
|
|
|
+ // 忽略回环接口和虚拟接口
|
|
|
+ if (networkInterface.isLoopback() || networkInterface.isVirtual()) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取MAC地址
|
|
|
+ byte[] mac = networkInterface.getHardwareAddress();
|
|
|
+ if (mac != null && mac.length > 0) {
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ for (int i = 0; i < mac.length; i++) {
|
|
|
+ sb.append(String.format("%02X:", mac[i]));
|
|
|
+ }
|
|
|
+ return sb.toString().substring(0, sb.length() - 1);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 如果没有找到可用的MAC地址,则返回本机IP地址作为设备ID
|
|
|
+ return InetAddress.getLocalHost().getHostAddress();
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void writeTimeStampToFile(long timeStamp) {
|
|
|
+ try {
|
|
|
+ // 创建隐藏文件(如果不存在)
|
|
|
+ File file = new File("C:\\time.txt");
|
|
|
+ file.createNewFile();
|
|
|
+ file.setWritable(true, false); // 设置文件为只写模式
|
|
|
+
|
|
|
+ // 将时间戳写入文件
|
|
|
+ FileWriter writer = new FileWriter(file);
|
|
|
+ writer.write(String.valueOf(timeStamp));
|
|
|
+ writer.close();
|
|
|
+
|
|
|
+ System.out.println("Time stamp has been written to the file.");
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取设备id(真实设备id+timestamp 加密)
|
|
|
+ * @return getDeviceId
|
|
|
+ */
|
|
|
+ public static String getDeviceId() {
|
|
|
+ String deviceId = getRealDeviceId();
|
|
|
+ String filePath = "C:\\time.txt";
|
|
|
+
|
|
|
+ File file = new File(filePath);
|
|
|
+ if (!file.exists()) {
|
|
|
+ // 文件不存在,写入时间戳到隐藏文件
|
|
|
+ long timeStamp = System.currentTimeMillis();
|
|
|
+ writeTimeStampToFile(timeStamp);
|
|
|
+
|
|
|
+ return deviceId + "-" + timeStamp;
|
|
|
+ } else {
|
|
|
+ // 文件存在,读取时间戳并与设备ID拼接返回
|
|
|
+ try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
|
|
|
+ String timeStampStr = reader.readLine();
|
|
|
+
|
|
|
+ return deviceId + "-" + timeStampStr;
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 校验设备id
|
|
|
+ * @param deviceId 设备id
|
|
|
+ * @return boolean
|
|
|
+ */
|
|
|
+ public boolean checkDeviceId(String deviceId) {
|
|
|
+ String realDeviceId = getDeviceId();
|
|
|
+ return deviceId.equals(realDeviceId);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+}
|