Skip to content

Commit 49b6dc9

Browse files
committed
整理公共方法
1 parent 5f9c686 commit 49b6dc9

File tree

4 files changed

+404
-231
lines changed

4 files changed

+404
-231
lines changed
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
package info.xiaomo.core.untils;
2+
3+
import java.util.regex.Matcher;
4+
import java.util.regex.Pattern;
5+
6+
/**
7+
* 把今天最好的表现当作明天最新的起点..~
8+
* いま 最高の表現 として 明日最新の始発..~
9+
* Today the best performance as tomorrow newest starter!
10+
* Created by IntelliJ IDEA.
11+
* <p>
12+
* author: xiaomo
13+
* github: https://github.com/syoubaku
14+
* email: xiaomo@xiamoo.info
15+
* QQ_NO: 83387856
16+
* Date: 17/5/11 17:57
17+
* Description:
18+
* Copyright(©) 2017 by xiaomo.
19+
*/
20+
public class HtmlUtil {
21+
22+
23+
/**
24+
* 是否包含有HTML标签
25+
*
26+
* @param str 字符串
27+
* @return 是否包含html标签
28+
*/
29+
public static boolean containsHTMLTag(String str) {
30+
if (StringUtil.isBlank(str)) {
31+
return false;
32+
}
33+
34+
String pattern = "<\\s*(\\S+)(\\s[^>]*)?>[\\s\\S]*<\\s*/\\1\\s*>";
35+
Pattern p = Pattern.compile(pattern);
36+
Matcher m = p.matcher(str);
37+
return m.find();
38+
}
39+
40+
/**
41+
* 用于将字符串中的特殊字符转换成Web页中可以安全显示的字符串
42+
* 可对表单数据据进行处理对一些页面特殊字符进行处理如'<','>','"',''','&'
43+
*
44+
* @param strSrc 要进行替换操作的字符串
45+
* @return 替换特殊字符后的字符串
46+
* @since 1.0
47+
*/
48+
49+
public static String htmlEncode(String strSrc) {
50+
if (strSrc == null)
51+
return "";
52+
char[] arr_cSrc = strSrc.toCharArray();
53+
StringBuilder buf = new StringBuilder(arr_cSrc.length);
54+
char ch;
55+
for (char anArr_cSrc : arr_cSrc) {
56+
ch = anArr_cSrc;
57+
58+
if (ch == '<')
59+
buf.append("&lt;");
60+
else if (ch == '>')
61+
buf.append("&gt;");
62+
else if (ch == '"')
63+
buf.append("&quot;");
64+
else if (ch == '\'')
65+
buf.append("&#039;");
66+
else if (ch == '&')
67+
buf.append("&amp;");
68+
else
69+
buf.append(ch);
70+
}
71+
72+
return buf.toString();
73+
}
74+
75+
/**
76+
* 用于将字符串中的特殊字符转换成Web页中可以安全显示的字符串
77+
* 可对表单数据据进行处理对一些页面特殊字符进行处理如'<','>','"',''','&'
78+
*
79+
* @param strSrc 要进行替换操作的字符串
80+
* @param quotes 为0时单引号和双引号都替换,为1时不替换单引号,为2时不替换双引号,为3时单引号和双引号都不替换
81+
* @return 替换特殊字符后的字符串
82+
* @since 1.0
83+
*/
84+
public static String htmlEncode(String strSrc, int quotes) {
85+
86+
if (strSrc == null)
87+
return "";
88+
if (quotes == 0) {
89+
return htmlEncode(strSrc);
90+
}
91+
92+
char[] arr_cSrc = strSrc.toCharArray();
93+
StringBuffer buf = new StringBuffer(arr_cSrc.length);
94+
char ch;
95+
96+
for (int i = 0; i < arr_cSrc.length; i++) {
97+
ch = arr_cSrc[i];
98+
if (ch == '<')
99+
buf.append("&lt;");
100+
else if (ch == '>')
101+
buf.append("&gt;");
102+
else if (ch == '"' && quotes == 1)
103+
buf.append("&quot;");
104+
else if (ch == '\'' && quotes == 2)
105+
buf.append("&#039;");
106+
else if (ch == '&')
107+
buf.append("&amp;");
108+
else
109+
buf.append(ch);
110+
}
111+
112+
return buf.toString();
113+
}
114+
115+
/**
116+
* 和htmlEncode正好相反
117+
*
118+
* @param strSrc 要进行转换的字符串
119+
* @return 转换后的字符串
120+
* @since 1.0
121+
*/
122+
public static String htmlDecode(String strSrc) {
123+
if (strSrc == null)
124+
return "";
125+
strSrc = strSrc.replaceAll("&lt;", "<");
126+
strSrc = strSrc.replaceAll("&gt;", ">");
127+
strSrc = strSrc.replaceAll("&quot;", "\"");
128+
strSrc = strSrc.replaceAll("&#039;", "'");
129+
strSrc = strSrc.replaceAll("&amp;", "&");
130+
return strSrc;
131+
}
132+
133+
/**
134+
* 去除html tag
135+
*
136+
* @param htmlStr html
137+
* @return string
138+
*/
139+
public static String delHTMLTag(String htmlStr) {
140+
String regEx_script = "<script[^>]*?>[\\s\\S]*?<\\/script>"; //定义script的正则表达式
141+
String regEx_style = "<style[^>]*?>[\\s\\S]*?<\\/style>"; //定义style的正则表达式
142+
String regEx_html = "<[^>]+>"; //定义HTML标签的正则表达式
143+
144+
Pattern p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE);
145+
Matcher m_script = p_script.matcher(htmlStr);
146+
htmlStr = m_script.replaceAll(""); //过滤script标签
147+
148+
Pattern p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE);
149+
Matcher m_style = p_style.matcher(htmlStr);
150+
htmlStr = m_style.replaceAll(""); //过滤style标签
151+
152+
Pattern p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
153+
Matcher m_html = p_html.matcher(htmlStr);
154+
htmlStr = m_html.replaceAll(""); //过滤html标签
155+
156+
return htmlStr.trim(); //返回文本字符串
157+
}
158+
159+
}

core/src/main/java/info/xiaomo/core/untils/RandomUtil.java

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77
package info.xiaomo.core.untils;
88

9+
import org.apache.commons.lang3.RandomStringUtils;
910
import org.slf4j.Logger;
1011
import org.slf4j.LoggerFactory;
1112

@@ -19,7 +20,8 @@
1920
*/
2021
public class RandomUtil {
2122
private static final Logger LOGGER = LoggerFactory.getLogger(RandomUtil.class);
22-
23+
private static final String NUM_S = "0123456789";
24+
private static final String STR_S = "abcdefghijklmnopqrstuvwxyz0123456789";
2325
/**
2426
* 随机产生min到max之间的一个整数值,包含min和max
2527
*/
@@ -61,6 +63,34 @@ public static <T> T randomElement(Collection<T> collection) {
6163
return null;
6264
}
6365

66+
67+
/**
68+
* 生成一个10位的tonken用于http cache(纯数字)
69+
*
70+
* @return String 返回类型(纯数字)
71+
*/
72+
public static String getTonken() {
73+
return RandomStringUtils.random(10, NUM_S);
74+
}
75+
76+
/**
77+
* 生成随机数
78+
*
79+
* @return String 返回类型
80+
*/
81+
public static String randomPwd(int count) {
82+
return RandomStringUtils.random(count, STR_S);
83+
}
84+
85+
/**
86+
* 生成随机数
87+
*
88+
* @return String 返回类型
89+
*/
90+
public static String randomPwd() {
91+
return RandomStringUtils.random(10, STR_S);
92+
}
93+
6494
/**
6595
* 从指定的元素数组中随机出一个元素
6696
*
@@ -121,19 +151,22 @@ public static int randomIndexByProb(int[] array) {
121151
return randomIndexByProb(list);
122152
}
123153

154+
/**
155+
* 生成盐值
156+
* @return
157+
*/
124158
public static String createSalt() {
125-
String[] str = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
126-
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j",
127-
"k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"};
128-
int number = str.length;
129-
//接收随机字符
130-
String text = "";
131-
Random random = new Random();
132-
//随机产生4个字符的字符串
133-
for (int i = 0; i < 10; i++) {
134-
text += str[random.nextInt(number)];
135-
}
136-
return text;
159+
return randomPwd(10);
160+
}
161+
162+
163+
/**
164+
* 生成盐值
165+
* @param count
166+
* @return
167+
*/
168+
public static String createSalt(int count) {
169+
return randomPwd(count);
137170
}
138171

139172
public static void main(String[] args) {
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package info.xiaomo.core.untils;
2+
3+
import org.apache.commons.lang3.StringUtils;
4+
5+
import java.util.Arrays;
6+
import java.util.StringTokenizer;
7+
8+
/**
9+
* 把今天最好的表现当作明天最新的起点..~
10+
* いま 最高の表現 として 明日最新の始発..~
11+
* Today the best performance as tomorrow newest starter!
12+
* Created by IntelliJ IDEA.
13+
* <p>
14+
* author: xiaomo
15+
* github: https://github.com/syoubaku
16+
* email: xiaomo@xiamoo.info
17+
* QQ_NO: 83387856
18+
* Date: 17/5/11 17:56
19+
* Description:
20+
* Copyright(©) 2017 by xiaomo.
21+
*/
22+
public class SqlUtil {
23+
24+
25+
/**
26+
* 功能描述: 生成sql占位符 ?,?,?
27+
*
28+
* @return String 返回类型
29+
*/
30+
public static String sqlHolder(int size) {
31+
String[] paras = new String[size];
32+
Arrays.fill(paras, "?");
33+
return StringUtils.join(paras, ',');
34+
}
35+
36+
/*
37+
*
38+
*/
39+
private String delSQlString(String sql) {
40+
StringBuilder delSql = new StringBuilder("in(");
41+
StringTokenizer Tokenizer = new StringTokenizer(sql, "|");
42+
43+
// 标记本身等于分隔符的特殊情况
44+
delSql.append(Tokenizer.nextToken());
45+
while (Tokenizer.hasMoreTokens()) {
46+
delSql.append(Tokenizer.nextToken()).append(",");
47+
}
48+
delSql = new StringBuilder(delSql.substring(0, delSql.length() - 1) + ")");
49+
return delSql.toString();
50+
}
51+
52+
private String delNewSQlString(String sql) {
53+
return "in (" + sql.replace('|', ',') + ")";
54+
}
55+
56+
/**
57+
* sql语句 处理
58+
*
59+
* @param sql 要进行处理的sql语句
60+
* @param dbtype 数据库类型
61+
* @return 处理后的sql语句
62+
*/
63+
public static String sql4DB(String sql, String dbtype) {
64+
if (!dbtype.equalsIgnoreCase("oracle")) {
65+
sql = StringUtil.toISO(sql);
66+
}
67+
return sql;
68+
}
69+
}

0 commit comments

Comments
 (0)