Explore Enterprise Education Gitee Premium Gitee AI AI teammates
Fetch the repository succeeded.
Donate
Please sign in before you donate.
Scan WeChat QR to Pay
Cancel
Complete
Prompt
Switch to Alipay.
OK
Cancel
1 Star 0 Fork 0

androidjp/ValidateCode

Create your Gitee Account
Explore and code with more than 14 million developers,Free private repositories !:)
Sign up
Already have an account? Sign in
文件
master
Branches (1)
master
This repository doesn't specify license. Please pay attention to the specific project description and its upstream code dependency when using it.
The license selected for the repository is subject to the license used by the main branch of the repository.
master
Branches (1)
master
Clone or Download
Clone/Download
Prompt
To download the code, please copy the following command and execute it in the terminal
To ensure that your submitted code identity is correctly recognized by Gitee, please execute the following command.
When using the SSH protocol for the first time to clone or push code, follow the prompts below to complete the SSH configuration.
1 Generate RSA keys.
2 Obtain the content of the RSA public key and configure it in SSH Public Keys
To use SVN on Gitee, please visit the usage guide
When using the HTTPS protocol, the command line will prompt for account and password verification as follows. For security reasons, Gitee recommends configure and use personal access tokens instead of login passwords for cloning, pushing, and other operations.
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # Private Token
master
Branches (1)
master
ValidateCode
/
src
/
util
/
StringRandomUtils.java
ValidateCode
/
src
/
util
/
StringRandomUtils.java
StringRandomUtils.java 5.42 KB
Copy Edit Raw Blame History
androidjp authored 2017年08月11日 18:07 +08:00 . 图片登录验证码
package util;
import java.io.UnsupportedEncodingException;
import java.util.Random;
public class StringRandomUtils {
private Random widthRandom = new Random();
private int length;
private static char[] charsNumber = ("0123456789").toCharArray();
private static char[] charsLetter = ("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ").toCharArray();
private static char[] charsRandom = ("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ").toCharArray();
private static Random random = new Random();
private final static int[] areaCode = {1601, 1637, 1833, 2078, 2274,
2302, 2433, 2594, 2787, 3106, 3212, 3472, 3635, 3722, 3730, 3858,
4027, 4086, 4390, 4558, 4684, 4925, 5249, 5590};
private final static String[] letters = {"a", "b", "c", "d", "e",
"f", "g", "h", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",
"t", "w", "x", "y", "z"};
//参数为生成的字符串的长度,根据给定的char集合生成字符串数字
public static String randomNumber(int length) {
char[] data = new char[length];
for (int i = 0; i < length; i++) {
int index = random.nextInt(charsNumber.length);
data[i] = charsNumber[index];
}
String s = new String(data);
return s;
}
//参数为生成的字符串的长度,根据给定的char集合生成字符串、字母
public static String randomLetter(int length) {
char[] data = new char[length];
for (int i = 0; i < length; i++) {
int index = random.nextInt(charsLetter.length);
data[i] = charsLetter[index];
}
String s = new String(data);
return s;
}
//参数为生成的字符串的长度,根据给定的char集合生成字符串
public static String getStringRandom(int length) {
char[] data = new char[length];
for (int i = 0; i < length; i++) {
int index = random.nextInt(charsRandom.length);
data[i] = charsRandom[index];
}
String s = new String(data);
return s;
}
//简体中文
public static String getRandomJianHan(int length) {
String ret = "";
for (int i = 0; i < length; i++) {
String str = null;
int hightPos, lowPos;//定义高地位
Random random = new Random();
hightPos = (176 + Math.abs(random.nextInt(39))); //获取高位值
lowPos = (161 + Math.abs(random.nextInt(93))); //获取低位值
byte[] b = new byte[2];
b[0] = (new Integer(hightPos).byteValue());
b[1] = (new Integer(lowPos).byteValue());
try {
str = new String(b, "GBK");//转成中文
} catch (UnsupportedEncodingException ex) {
ex.printStackTrace();
}
ret += str;
}
return ret;
}
/*封装成带下划线的蓝色文本*/
public static String toBlueTextUnderLine(String text) {
return "<u><font color=\"#15ABF9\">" + text + "</font></u>";
}
/**
* 获取汉字的首字母
* @param x
* @return
*/
public static String getFirstAlp(String x) {
String chinese = x.substring(0,1);
if (chinese == null || chinese.trim().length() == 0) {
return "";
}
chinese = conversionStr(chinese, "GB2312", "ISO8859-1");
if (chinese.length() > 1) // 判断是不是汉字
{
int li_SectorCode = (int) chinese.charAt(0); // 汉字区码
int li_PositionCode = (int) chinese.charAt(1); // 汉字位码
li_SectorCode = li_SectorCode - 160;
li_PositionCode = li_PositionCode - 160;
int li_SecPosCode = li_SectorCode * 100 + li_PositionCode; // 汉字区位码
if (li_SecPosCode > 1600 && li_SecPosCode < 5590) {
// ToastUtils.showShort(MyApplication.getContext(),"chinese 是汉字字符");
for (int i = 0; i < 23; i++) {
if (li_SecPosCode >= areaCode[i]
&& li_SecPosCode < areaCode[i + 1]) {
chinese = letters[i];
break;
}
}
} else // 非汉字字符,如图形符号或ASCII码
{
// ToastUtils.showShort(MyApplication.getContext(),"chinese 不是汉字!!");
chinese = conversionStr(chinese, "ISO8859-1", "GB2312");
chinese = chinese.substring(0, 1);
}
}
return chinese;
}
/**
* 字符串编码转换
*
* @param str 要转换编码的字符串
* @param charsetName 原来的编码
* @param toCharsetName 转换后的编码
* @return 经过编码转换后的字符串
*/
private static String conversionStr(String str, String charsetName, String toCharsetName) {
try {
str = new String(str.getBytes(charsetName), toCharsetName);
} catch (UnsupportedEncodingException ex) {
System.out.println("字符串编码转换异常:" + ex.getMessage());
}
return str;
}
}
Loading...
Report
Report success
We will send you the feedback within 2 working days through the letter!
Please fill in the reason for the report carefully. Provide as detailed a description as possible.
Please select a report type
Cancel
Send
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

Releases

No release

Contributors

All

Activities

can not load any more
Edit
About
Homepage
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/androidJP/ValidateCode.git
git@gitee.com:androidJP/ValidateCode.git
androidJP
ValidateCode
ValidateCode
master
Going to Help Center

Search

Comment
Repository Report
Back to the top
Login prompt
This operation requires login to the code cloud account. Please log in before operating.
Go to login
No account. Register

AltStyle によって変換されたページ (->オリジナル) /