Repository navigation
Expand file tree
/
Copy pathCodecUtil.java
More file actions
110 lines (97 loc) · 2.22 KB
/
Copy pathCodecUtil.java
File metadata and controls
110 lines (97 loc) · 2.22 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
package bms.core.common.util;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.UUID;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.RandomStringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* 编码与解码操作工具类
*
* @author huangyong
* @since 1.0
*/
public class CodecUtil {
static String UTF_8 = "UTF-8";
private static final Logger logger = LoggerFactory
.getLogger(CodecUtil.class);
/**
* 将 URL 编码
*/
public static String encodeURL(String str) {
String target;
try {
target = URLEncoder.encode(str, UTF_8);
} catch (Exception e) {
logger.error("编码出错!", e);
throw new RuntimeException(e);
}
return target;
}
/**
* 将 URL 解码
*/
public static String decodeURL(String str) {
String target;
try {
target = URLDecoder.decode(str, UTF_8);
} catch (Exception e) {
logger.error("解码出错!", e);
throw new RuntimeException(e);
}
return target;
}
/**
* 将字符串 Base64 编码
*/
public static String encodeBASE64(String str) {
String target;
try {
target = Base64.encodeBase64URLSafeString(str.getBytes(UTF_8));
} catch (UnsupportedEncodingException e) {
logger.error("编码出错!", e);
throw new RuntimeException(e);
}
return target;
}
/**
* 将字符串 Base64 解码
*/
public static String decodeBASE64(String str) {
String target;
try {
target = new String(Base64.decodeBase64(str), UTF_8);
} catch (UnsupportedEncodingException e) {
logger.error("解码出错!", e);
throw new RuntimeException(e);
}
return target;
}
/**
* 将字符串 MD5 加密
*/
public static String encryptMD5(String str) {
return DigestUtils.md5Hex(str);
}
/**
* 将字符串 SHA 加密
*/
public static String encryptSHA(String str) {
return DigestUtils.sha1Hex(str);
}
/**
* 创建随机数
*/
public static String createRandom(int count) {
return RandomStringUtils.randomNumeric(count);
}
/**
* 获取 UUID(32位)
*/
public static String createUUID() {
return UUID.randomUUID().toString().replaceAll("-", "");
}
}