Base64Util.java
1.31 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
package com.topdraw.util;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.Base64Utils;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.regex.Pattern;
public class Base64Util {
public static String encode(String name){
String name1 = new String(Base64.getEncoder().encode(name.getBytes(StandardCharsets.UTF_8)));
return name1;
}
public static boolean isBase64(String str) {
if (StringUtils.isBlank(str)) {
return false;
}
String base64Pattern = "^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)$";
boolean matches = Pattern.matches(base64Pattern, str);
if (matches) {
String decodeBase64Str = new String(Base64Utils.decode(str.getBytes()));
String encodeBase64Str = Base64Utils.encodeToString(decodeBase64Str.getBytes());
if (str.equals(encodeBase64Str)) {
return true;
}
}
return false;
}
public static void main(String[] args) {
// String name = "test005@itv";
// String name = "18580619168a@iptv";
// String encode = encode(name);
// System.out.println(encode);
boolean a = isBase64("fdsfdsf");
System.out.println(a);
}
}