码迷,mamicode.com
首页 > 编程语言 > 详细

Java开发工具类集合

时间:2020-11-01 22:06:45      阅读:33      评论:0      收藏:0      [点我收藏+]

标签: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 < bytes.length; i++) {
                byte b = bytes[i];
                chars[k++] = hexChars[b >>> 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<Object, Object> hashMap = new HashMap<Object, Object>();
 
		// 生成随机数字并存入HashMap
		for (int i = 0; i < list.size(); i++) {
			int number = random.nextInt(100) + 1;
			hashMap.put(number, i);
		}
 
		// 从HashMap导入数组
		Object[] robjs = hashMap.values().toArray();
		ArrayList r = new ArrayList();
		// 遍历数组并打印数据
		for (int i = 0; i < n; i++) {
			r.add(list.get((int) robjs[i]));
			System.out.print(list.get((int) robjs[i]) + "\t");
		}
		System.out.print("\n");
		return r;
	}
}
 

四、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 < 3; i++) {
            result += random.nextInt(10);
        }
        return newDate + result;
    }

}

Java开发工具类集合

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

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!