Java开发工具类集合

2021-03-20 21:26

阅读:767

标签:put   flush   mss   port   finally   加密   odi   bytes   开发   

Java开发工具类集合

一、MD5加密工具类

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
 
 
public final class MD5 {
 
    public static String encrypt(String strSrc) {
        try {
            char hexChars[] = { ‘0‘, ‘1‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘, ‘6‘, ‘7‘, ‘8‘,
                    ‘9‘, ‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘, ‘f‘ };
            byte[] bytes = strSrc.getBytes();
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(bytes);
            bytes = md.digest();
            int j = bytes.length;
            char[] chars = new char[j * 2];
            int k = 0;
            for (int i = 0; i >> 4 & 0xf];
                chars[k++] = hexChars[b & 0xf];
            }
            return new String(chars);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            throw new RuntimeException("MD5加密出错!!+" + e);
        }
    }
 
    public static void main(String[] args) {
        System.out.println(MD5.encrypt("111111"));
    }
 
}

二、手机号码校验工具类

import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
/**
 * 字符串操作工具类
 */
public class FormUtils {
 
    /**
     * 手机号验证
     */
    public static boolean isMobile(String str) {
        Pattern p = Pattern.compile("^[1][3,4,5,7,8,9][0-9]{9}$"); // 验证手机号
        Matcher m = p.matcher(str);
        return m.matches();
    }
 
}

三、验证码生成工具类

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Random;
 
/**
 * 获取随机数
 * 
 * @author qianyi
 *
 */
public class RandomUtils {
 
	private static final Random random = new Random();
 
	private static final DecimalFormat fourdf = new DecimalFormat("0000");
 
	private static final DecimalFormat sixdf = new DecimalFormat("000000");
 
	public static String getFourBitRandom() {
		return fourdf.format(random.nextInt(10000));
	}
 
	public static String getSixBitRandom() {
		return sixdf.format(random.nextInt(1000000));
	}
 
	/**
	 * 给定数组,抽取n个数据
	 * @param list
	 * @param n
	 * @return
	 */
	public static ArrayList getRandom(List list, int n) {
 
		Random random = new Random();
		HashMap hashMap = new HashMap();
 
		// 生成随机数字并存入HashMap
		for (int i = 0; i 

四、InputStream流转换成String字符串

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class StreamUtils {

    private static int _buffer_size = 1024;

    /**
     * InputStream流转换成String字符串
     * @param inStream InputStream流
     * @param encoding 编码格式
     * @return String字符串
     */
    public static String inputStream2String(InputStream inStream, String encoding){
        String result = null;
        ByteArrayOutputStream outStream = null;
        try {
            if(inStream != null){
                outStream = new ByteArrayOutputStream();
                byte[] tempBytes = new byte[_buffer_size];
                int count = -1;
                while((count = inStream.read(tempBytes, 0, _buffer_size)) != -1){
                    outStream.write(tempBytes, 0, count);
                }
                tempBytes = null;
                outStream.flush();
                result = new String(outStream.toByteArray(), encoding);

                outStream.close();
            }
        } catch (Exception e) {
            result = null;
        } finally {
            try {
                if(inStream != null) {
                    inStream.close();
                    inStream = null;
                }
                if(outStream != null) {
                    outStream.close();
                    outStream = null;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }
}

五、商品订单号生成工具类

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

/**
 * 订单号工具类
 *
 * @author qy
 * @since 1.0
 */
public class OrderNoUtils {

    /**
     * 获取订单号
     * @return
     */
    public static String getOrderNo() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String newDate = sdf.format(new Date());
        String result = "";
        Random random = new Random();
        for (int i = 0; i 

Java开发工具类集合

标签:put   flush   mss   port   finally   加密   odi   bytes   开发   

原文地址:https://www.cnblogs.com/smalldong/p/13909805.html


评论


亲,登录后才可以留言!