Utils.java 6.2 KB
package com.teplot.common;

import java.io.File;
import java.io.FileFilter;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.jfinal.kit.HashKit;
import com.jfinal.kit.PropKit;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;

/**
 * Depiction: 常用工具方法
 * <p>
 * Modify:
 * <p>
 * Author: Kevin Lynn
 * <p>
 * Create Date:2016年7月28日 下午2:17:59
 * <p>
 * 
 * @version 1.0
 * @since 1.0
 */
public final class Utils {
	public static void main(String[] args) {
		String password = HashKit.sha256("000000");
		System.out.println(password);
		System.out.println(password.length());
	}

	/**
	 * 加载配置文件
	 */
	public static void loadConfig() {
		PropKit.use("config.properties");
	}

	/**
	 * 批量导入sql文件
	 * 
	 * @param arp
	 * @param sqlBasePath
	 *            sql文件的根目录
	 */
	public static void loadSqls(ActiveRecordPlugin arp, String sqlBasePath) {
		File sqlBasePathDir = new File(sqlBasePath);
		if (sqlBasePathDir != null && sqlBasePathDir.exists() && sqlBasePathDir.isDirectory()) {
			arp.setBaseSqlTemplatePath(sqlBasePath);

			File[] sqls = sqlBasePathDir.listFiles(new FileFilter() {
				public boolean accept(File f) {
					if (f.isFile() && f.getName().endsWith(".sql")) {
						return true;
					}
					return false;
				}
			});
			for (File sql : sqls) {
				arp.addSqlTemplate(sql.getName());
			}
		}
	}

	public static int str2Int(String str) {
		int value = 0;
		try {
			value = Integer.parseInt(str);
		} catch (Exception e) {
		}
		return value;
	}
	
	public static float str2Float(String str) {
		float value = 0.0f;
		try {
			value = Float.parseFloat(str);
		} catch (Exception e) {
		}
		return value;
	}

	public static String subStr(String source, int number) {
		if (StrKit.isBlank(source)) {
			return "";
		}

		int length = source.length();
		if (length >= number) {
			source = source.substring(0, number);
		}
		return source;
	}

	public static int checkStock(String stock) {
		int tmp = Integer.parseInt(formatNumber(stock, 0));
		tmp = tmp > 9999 ? 9999 : tmp;
		tmp = tmp < 0 ? 0 : tmp;
		return tmp;
	}

	/**
	 * 格式化数字,为两位小数点的字符串
	 * 
	 * @param number
	 *            数字原始数据
	 * @param count
	 *            小数点位数
	 * @return String
	 */
	public static String formatNumber(String number, int count) {
		count = count >= 0 ? count : 0;

		float tmp = 0.f;
		try {
			tmp = Float.parseFloat(number);
		} catch (Exception e) {
		}
		DecimalFormat format = new DecimalFormat("0");
		if (count > 0) {
			StringBuffer pattern = new StringBuffer();
			pattern.append("0.");
			for (int i = 0; i < count; i++) {
				pattern.append("0");
			}
			format = new DecimalFormat(pattern.toString());
		}

		return format.format(tmp);
	}

	/**
	 * 解析价格的整数位和小数位
	 * 
	 * @param number
	 *            要解析的价格
	 * @return String[] index_0 整数位; index_1 小数位;
	 */
	public static String[] parseNumber(String number) {
		String tmp = formatNumber(number, 2);
		return tmp.split("\\.");
	}

	public static String productNo() {
		return new SimpleDateFormat("yyMMddHHmmssS").format(new Date());
	}

	public static String orderNo() {
		Random random = new Random();
		String rnumber = random.nextInt(9) + "" + random.nextInt(9) + "" + random.nextInt(9) + "" + random.nextInt(9);
		long time = System.currentTimeMillis();
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
		return format.format(new Date(time)) + "-" + rnumber;
	}

	public static String uuid() {
		return UUID.randomUUID().toString().replace("-", "");
	}

	public static String uuid(int number) {
		if (number < 1) {
			return null;
		}

		String uuid = uuid();
		return uuid.substring(0, number);
	}

	public static String formatTime(long time) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return format.format(new Date(time));
	}

	public static List<String> getImgFromHtml(String html) {
		if (StrKit.isBlank(html)) {
			return null;
		}

		List<String> imgList = new ArrayList<String>();

		Pattern p_image;
		Matcher m_image;

		String regEx_img = "(<img.*src\\s*=\\s*(.*?)[^>]*?>)";
		p_image = Pattern.compile(regEx_img, Pattern.CASE_INSENSITIVE);
		m_image = p_image.matcher(html);
		while (m_image.find()) {
			String img = m_image.group();
			Matcher m = Pattern.compile("src\\s*=\\s*\"?(.*?)(\"|>|\\s+)").matcher(img);
			while (m.find()) {
				String tempSelected = m.group(1);
				imgList.add(tempSelected);
			}
		}
		return imgList;
	}

	public static String host() {
		return PropKit.get("host");
	}

	public static String contextPathAdmin() {
		return PropKit.get("contextPathAdmin");
	}

	public static String contextPathApi() {
		return PropKit.get("contextPathApi");
	}

	public static String getBaseUploadDir() {
		return PropKit.get("baseUploadDir");
	}

	public static String getPortraitDir() {
		String temp = getBaseUploadDir() + "/portrait";
		Utils.mkdirs(temp);
		return temp;
	}

	public static String getImgDir() {
		String temp = getBaseUploadDir() + "/img";
		Utils.mkdirs(temp);
		return temp;
	}

	public static String getQRDir() {
		String temp = getBaseUploadDir() + "/qr";
		Utils.mkdirs(temp);
		return temp;
	}

	public static String getFileDir() {
		String temp = getBaseUploadDir() + "/file";
		Utils.mkdirs(temp);
		return temp;
	}

	public static boolean mkdirs(String dirPath) {
		boolean flag = false;
		File dir = new File(dirPath);
		if (!dir.exists()) {
			flag = dir.mkdirs();
		}
		return flag;
	}

	public static boolean deleteFile(String filePath) {
		File file = new File(filePath);
		if (file.exists()) {
			return file.delete();
		}

		return true;
	}

	public static void deleteDir(String dirPath) {
		File dir = new File(dirPath);
		if (dir.exists() && dir.isDirectory()) {
			File[] files = dir.listFiles();
			if (files != null) {
				for (File file : files) {
					if (file.exists() && file.isFile()) {
						file.delete();
					} else {
						deleteDir(file.getAbsolutePath());
					}
				}
			}
		}
	}

}