package com.ard.route.util;
|
|
import java.time.LocalDateTime;
|
import java.time.format.DateTimeFormatter;
|
|
/**
|
* 工具类
|
*/
|
public class ToolUtils {
|
|
public static Boolean isLocal = false;
|
|
public static String localFilePath = "KmzFile/kmz/";
|
|
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 "";
|
}
|
|
}
|