惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
The Blog of Author Tim Ferriss
S
Securelist
D
Docker
The Register - Security
The Register - Security
GbyAI
GbyAI
Recorded Future
Recorded Future
Engineering at Meta
Engineering at Meta
Stack Overflow Blog
Stack Overflow Blog
云风的 BLOG
云风的 BLOG
P
Proofpoint News Feed
罗磊的独立博客
博客园 - 【当耐特】
F
Full Disclosure
WordPress大学
WordPress大学
腾讯CDC
小众软件
小众软件
大猫的无限游戏
大猫的无限游戏
D
DataBreaches.Net
SecWiki News
SecWiki News
L
Lohrmann on Cybersecurity
I
InfoQ
MyScale Blog
MyScale Blog
量子位
Cyberwarzone
Cyberwarzone
博客园 - 三生石上(FineUI控件)
The Hacker News
The Hacker News
F
Fortinet All Blogs
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Jina AI
Jina AI
博客园_首页
H
Help Net Security
K
Kaspersky official blog
酷 壳 – CoolShell
酷 壳 – CoolShell
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Webroot Blog
Webroot Blog
Blog — PlanetScale
Blog — PlanetScale
V
Vulnerabilities – Threatpost
Y
Y Combinator Blog
The Cloudflare Blog
P
Proofpoint News Feed
V
Visual Studio Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
T
Tailwind CSS Blog
爱范儿
爱范儿
P
Privacy International News Feed
Security Archives - TechRepublic
Security Archives - TechRepublic
The GitHub Blog
The GitHub Blog
C
Cybersecurity and Infrastructure Security Agency CISA
B
Blog RSS Feed

博客园 - Bob.Xie

如何把一个本地项目上传到git MySQL导出数据字典 java 判断日期是否小于本月 vue 日期控件el-date-picker 获取选择月份的最后一天 vue 数据导入加载样式 CSS文字过多设置省略号 Spring 本地获取文件 mybatis 批量新增和修改 SpringBoot 全局统一异常拦截器 List stream 数据处理 MySQL中concat,concat_ws以及group_concat的使用 Java获取当前日期属于今年的第几周 使用tesseract-ocr读取图片文字(转) element-ui表格el-table回显时默认全选数据 idea 插件库 MySQL分组修改排序序号 查看Linux服务器连接数,Oracle表和索引分析 转载:查看Oracle连接数 转载:查看ORACLE最耗时的SQL
生成随机字符串 工具类
Bob.Xie · 2022-01-17 · via 博客园 - Bob.Xie

生成随机字符串 工具类

package com.shop.cyshop.commons.utils;

import java.util.Date;
import java.util.Random;
import java.util.UUID;

/**
 * @Name RandomStringUtil
 * @Descr 生成随机字符串
 * @author lnexin@aliyun.com
 * @date 2015年10月15日下午2:36:05
 */
public class RandomStringUtil {
    /**
     * @param passLength
     *            : 要生成多少长度的字符串
     * @param type
     *            : 需要哪种类型
     * @return 根据传入的type来判定
     */
    
    // 可以根据自己需求来删减下面的代码,不要要的类型可以删掉
     
    // type=0:纯数字(0-9)
    // type=1:全小写字母(a-z)
    // type=2:全大写字母(A-Z)
    // type=3: 数字+小写字母
    // type=4: 数字+大写字母
    // type=5:大写字母+小写字母
    // type=6:数字+大写字母+小写字母
    // type=7:固定长度33位:根据UUID拿到的随机字符串,去掉了四个"-"(相当于长度33位的小写字母加数字)

    public static String getRandomCode(int passLength, int type) {
        StringBuffer buffer = null;
        StringBuffer sb = new StringBuffer();
        Random r = new Random();
        r.setSeed(new Date().getTime());
        switch (type) {
        case 0:
            buffer = new StringBuffer("123456789");
            break;
        case 1:
            buffer = new StringBuffer("abcdefghijklmnopqrstuvwxyz");
            break;
        case 2:
            buffer = new StringBuffer("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
            break;
        case 3:
            buffer = new StringBuffer("0123456789abcdefghijklmnopqrstuvwxyz");
            break;
        case 4:
            buffer = new StringBuffer("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ");
            break;
        case 5:
            buffer = new StringBuffer("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
            break;
        case 6:
            buffer = new StringBuffer("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
            sb.append(buffer.charAt(r.nextInt(buffer.length() - 10)));
            passLength -= 1;
            break;
        case 7:
            String s = UUID.randomUUID().toString();
            sb.append(s.substring(0, 8) + s.substring(9, 13) + s.substring(14, 18) + s.substring(19, 23) + s.substring(24));
        }

        if (type != 7) {
            int range = buffer.length();
            for (int i = 0; i < passLength; ++i) {
                sb.append(buffer.charAt(r.nextInt(range)));
            }
        }
        return sb.toString();
    }


    public static String getRandom() {
        String num = "";
        for (int i = 0; i < 4; i++) {
            num = num + String.valueOf((int) Math.floor(Math.random() * 9 + 1));
        }
        return num;
    }
}

posted @ 2022-01-17 18:44  Bob.Xie  阅读(102)  评论()    收藏  举报