# 获取图片URL格式
本文作者:程序员飞云
# 使用HEAD请求获取图片格式
long ONE_M = (1024 * 1024);
// 图片上传类型
List<String> IMAGE_TYPE = Arrays.asList("jpg", "jpeg", "png", "gif", "bmp", "webp");
List<String> ALLOW_CONTENT_TYPE = Arrays.asList("image/jpeg", "image/png", "image/gif", "image/bmp", "image/webp");
1
2
3
4
5
2
3
4
5
HttpResponse response = null;
try {
response = HttpUtil.createRequest(Method.HEAD, fileUrl).execute();
if (response.getStatus() != HttpStatus.HTTP_OK) {
return;
}
// 4. 校验图片类型是否合法
String contentType = response.header("content-type");
if (StrUtil.isNotBlank(contentType)) {
if (!ALLOW_CONTENT_TYPE.contains(contentType.toLowerCase())) {
throw new BusinessException(ErrorCode.PARAMS_ERROR, "图片格式不正确");
}
}
// 5. 校验图片大小是否合法
String contentLengthStr = response.header("content-length");
if (!StrUtil.isBlank(contentLengthStr)) {
try {
long contentLength = Long.parseLong(contentLengthStr);
if (contentLength > ONE_M * 2) {
throw new BusinessException(ErrorCode.PARAMS_ERROR, "图片大小超过2M");
}
} catch (NumberFormatException e) {
throw new BusinessException(ErrorCode.PARAMS_ERROR, "图片大小格式错误");
}
}
} catch (Exception e) {
throw new BusinessException(ErrorCode.PARAMS_ERROR, "图片校验失败");
} finally {
if (response != null) {
response.close();
}
}
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
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