Xiajijian
2025-12-13 d622682904bb0a4b401db317e8d0b9fdf474e54b
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package com.ard.route.util;
 
import com.ard.route.domain.djuav.UavRouteReq;
 
import java.io.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
 
/**
 * 工具类
 */
public class ToolUtils {
 
    public static String kmzPath = "kmz";
 
    /**
     * XML 头部
     */
    public static final String XML_HEADER = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
 
    /**
     * 生成KMZ文件名称规则
     */
    public static String buildKmzFileName() {
        // 获取当前日期时间
        LocalDateTime now = LocalDateTime.now();
        return "测试KMZ文件" + "_" + now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd_HH-mm-ss"));
    }
 
    /**
     * 根据路径截取文件名
     */
    public static String getFileNameWithoutExtension(String filePath) {
        if (filePath == null || filePath.trim().isEmpty()) {
            return "";
        }
 
        // 规范化路径,处理正斜杠和反斜杠
        String normalizedPath = filePath.replace('\\', '/');
 
        // 获取文件名(含扩展名)
        String fileNameWithExt = normalizedPath.substring(normalizedPath.lastIndexOf('/') + 1);
 
        // 去除扩展名
        int lastDotIndex = fileNameWithExt.lastIndexOf('.');
        if (lastDotIndex > 0) {
            return fileNameWithExt.substring(0, lastDotIndex);
        }
 
        return fileNameWithExt;
    }
 
    /**
     * 从URL中提取文件路径部分
     * @param url 完整的URL字符串
     * @return 文件路径部分
     */
    public static String extractFilePath(String url) {
        if (url == null || url.isEmpty()) {
            return "";
        }
 
        // 找到最后一个 "kmz/" 的位置,加上4以包含 "kmz/"
        int index = url.lastIndexOf("kmz/");
        if (index != -1) {
            return url.substring(index + 4); // +4 跳过 "kmz/"
        }
 
        // 如果没有找到 "kmz/",尝试其他方式
        return extractPathByDomain(url);
    }
 
    /**
     * 备选方法:通过域名后的第一个斜杠来提取
     */
    private static String extractPathByDomain(String url) {
        // 移除协议头
        String withoutProtocol = url.replaceFirst("^https?://", "");
        // 找到第一个斜杠位置
        int slashIndex = withoutProtocol.indexOf("/");
        if (slashIndex != -1) {
            return withoutProtocol.substring(slashIndex + 1);
        }
        return "";
    }
 
    // 对象序列化为字节数组
    public static byte[] serialize(Object obj) throws IOException {
        try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
             ObjectOutputStream oos = new ObjectOutputStream(baos)) {
            oos.writeObject(obj);
            return baos.toByteArray();
        }
    }
 
    // 字节数组反序列化为对象
    public static Object deserialize(byte[] data) throws IOException, ClassNotFoundException {
        try (ByteArrayInputStream bais = new ByteArrayInputStream(data);
             ObjectInputStream ois = new ObjectInputStream(bais)) {
            return ois.readObject();
        }
    }
 
    /**
     * 截取字符串
     * 参数: https://xxx.xxx.xxx.xxx:xxx/kmz/2025/11/08/文件名_20251108152249A001.kmz
     */
    public static String subStringMinioName(String url) {
        return new File(url).getName();
    }
 
    /**
     * 截取字符串
     * minio里面文件夹路径名称
     */
    public static String subStringMinioUrlName(String url) {
        int kmzIndex = url.indexOf("/kmz/");
        return url.substring(kmzIndex + 5); // +5 是为了跳过 "/kmz/"
    }
 
    /**
     * 截取字符串
     * 参数: "0-89-0"
     */
    public static Integer subStringDroneType(String str) {
        String[] parts = str.split("-");
        return Integer.valueOf(parts[1]);
    }
 
    /**
     * 截取字符串
     * 获取经纬度高度
     */
    public static Double getDoublePoint(String str,String type) {
        if (str == null || str.isEmpty()) {
            return null;
        }
        String[] parts = str.split(",");
        if ("lon".equals(type)) {
            return Double.parseDouble(parts[0].trim());
        }
        if ("lat".equals(type)){
            return Double.parseDouble(parts[1].trim());
        }
        if ("alt".equals(type)){
            return Double.parseDouble(parts[2].trim());
        }
        return null;
    }
 
    /**
     * 获取航线类型方法
     */
    public static String templateTypeData(UavRouteReq uavRouteReq) {
        if ("waypoint".equals(uavRouteReq.getTemplateType())){
            return "0";
        }
        return "1";
    }
 
    /**
     * 获取无人机类型方法
     */
    public static String droneTypeData(UavRouteReq uavRouteReq) {
        return "0-" + uavRouteReq.getDroneType() + "-0";
    }
 
}