FileUrlUtils.java
6.26 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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package com.bs.common.utils.file;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
/**
* 文件URL工具类 - 获取音视频图片链接的文件信息
*/
public class FileUrlUtils {
// 支持的音视频格式
private static final String[] AUDIO_EXTENSIONS = {
"mp3", "wav", "wma", "aac", "flac", "ogg", "m4a", "aiff", "ape"
};
private static final String[] VIDEO_EXTENSIONS = {
"mp4", "avi", "mkv", "mov", "wmv", "flv", "webm", "m4v", "3gp",
"mpg", "mpeg", "rm", "rmvb", "ts", "mts"
};
private static final String[] IMAGE_EXTENSIONS = {
"jpg", "jpeg", "png", "gif", "bmp", "webp", "svg", "ico",
"tiff", "tif", "psd", "ai", "eps"
};
// 文件类型枚举
public enum FileType {
AUDIO,
VIDEO,
IMAGE,
UNKNOWN
}
/**
* 从URL中提取文件信息
*
* @param fileUrl 文件URL
* @return 文件信息Map,包含文件名、后缀、类型等
*/
public static Map<String, Object> getFileInfoFromUrl(String fileUrl) {
Map<String, Object> fileInfo = new HashMap<>();
if (fileUrl == null || fileUrl.trim().isEmpty()) {
fileInfo.put("error", "URL不能为空");
return fileInfo;
}
try {
URL url = new URL(fileUrl);
String path = url.getPath();
// 提取文件名和后缀
extractFileNameAndExtension(path, fileInfo);
// 判断文件类型
determineFileType(fileInfo);
// 添加其他信息
fileInfo.put("fullUrl", fileUrl);
fileInfo.put("domain", url.getHost());
fileInfo.put("path", path);
fileInfo.put("query", url.getQuery());
} catch (MalformedURLException e) {
fileInfo.put("error", "URL格式错误: " + e.getMessage());
} catch (Exception e) {
fileInfo.put("error", "解析URL失败: " + e.getMessage());
}
return fileInfo;
}
/**
* 提取文件名和后缀
*/
private static void extractFileNameAndExtension(String path, Map<String, Object> fileInfo) {
if (path == null || path.isEmpty()) {
fileInfo.put("error", "路径为空");
return;
}
// 处理包含查询参数的URL(如:image.jpg?width=100)
String cleanPath = path;
int queryIndex = path.indexOf('?');
if (queryIndex != -1) {
cleanPath = path.substring(0, queryIndex);
}
// 获取文件名(最后一个/之后的内容)
int lastSlashIndex = cleanPath.lastIndexOf('/');
String fileNameWithExt = lastSlashIndex != -1 ?
cleanPath.substring(lastSlashIndex + 1) : cleanPath;
if (fileNameWithExt.isEmpty()) {
fileInfo.put("error", "无法提取文件名");
return;
}
// 分离文件名和后缀
int lastDotIndex = fileNameWithExt.lastIndexOf('.');
if (lastDotIndex == -1) {
// 没有后缀
fileInfo.put("fileName", fileNameWithExt);
fileInfo.put("extension", "");
fileInfo.put("fullName", fileNameWithExt);
} else {
// 有后缀
String fileName = fileNameWithExt.substring(0, lastDotIndex);
String extension = fileNameWithExt.substring(lastDotIndex + 1).toLowerCase();
fileInfo.put("fileName", fileName);
fileInfo.put("extension", extension);
fileInfo.put("fullName", fileNameWithExt);
}
}
/**
* 判断文件类型
*/
private static void determineFileType(Map<String, Object> fileInfo) {
String extension = (String) fileInfo.get("extension");
if (extension == null || extension.isEmpty()) {
fileInfo.put("fileType", FileType.UNKNOWN);
return;
}
// 检查图片类型
for (String ext : IMAGE_EXTENSIONS) {
if (ext.equalsIgnoreCase(extension)) {
fileInfo.put("fileType", FileType.IMAGE);
return;
}
}
// 检查音频类型
for (String ext : AUDIO_EXTENSIONS) {
if (ext.equalsIgnoreCase(extension)) {
fileInfo.put("fileType", FileType.AUDIO);
return;
}
}
// 检查视频类型
for (String ext : VIDEO_EXTENSIONS) {
if (ext.equalsIgnoreCase(extension)) {
fileInfo.put("fileType", FileType.VIDEO);
return;
}
}
fileInfo.put("fileType", FileType.UNKNOWN);
}
/**
* 判断是否为音频文件
*/
public static boolean isAudioFile(String fileUrl) {
Map<String, Object> info = getFileInfoFromUrl(fileUrl);
return FileType.AUDIO.equals(info.get("fileType"));
}
/**
* 判断是否为视频文件
*/
public static boolean isVideoFile(String fileUrl) {
Map<String, Object> info = getFileInfoFromUrl(fileUrl);
return FileType.VIDEO.equals(info.get("fileType"));
}
/**
* 判断是否为图片文件
*/
public static boolean isImageFile(String fileUrl) {
Map<String, Object> info = getFileInfoFromUrl(fileUrl);
return FileType.IMAGE.equals(info.get("fileType"));
}
/**
* 获取文件后缀(快捷方法)
*/
public static String getFileExtension(String fileUrl) {
Map<String, Object> info = getFileInfoFromUrl(fileUrl);
return (String) info.get("extension");
}
/**
* 获取文件名(不含后缀)
*/
public static String getFileName(String fileUrl) {
Map<String, Object> info = getFileInfoFromUrl(fileUrl);
return (String) info.get("fileName");
}
/**
* 获取完整文件名(含后缀)
*/
public static String getFullFileName(String fileUrl) {
Map<String, Object> info = getFileInfoFromUrl(fileUrl);
return (String) info.get("fullName");
}
public static void main(String[] args) {
System.out.println(getFullFileName("https://report-default-test.oss-cn-shenzhen.aliyuncs.com/2025/11/21/2ec9f621-5aa1-417f-a0c4-5970ab204038.jpg"));
}
}