using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace AIK.Common.WebHelper { public static class UrlHelper { /// /// 从完整URL中提取文件名(保留URL编码,如 %28 不会转成 () /// /// 完整的下载链接 /// URL编码的文件名,如 "20251126055829_721_%28K3_TOOL%291.2.9.bin" public static string GetEncodedFileNameFromUrl(string url) { if (string.IsNullOrWhiteSpace(url)) return string.Empty; try { var uri = new Uri(url); // Segments 是路径按 '/' 分割的数组,最后一个元素即为文件名(含原始编码) string[] segments = uri.Segments; if (segments.Length == 0) return string.Empty; string fileName = segments[segments.Length - 1]; // 移除末尾可能的斜杠(虽然文件名通常不会有) return fileName.TrimEnd('/'); } catch (UriFormatException) { // URL 格式无效时返回空或抛异常,根据需求调整 return string.Empty; } } } }