工具类

package com.liam.util;

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

/**
 * @author liam.huang@foxmail.com
 * 基础工具类
 */
public class BasicUtil {
	
	/**
	 * 1.判断是否“不为空”
	 * @param ob
	 * @return
	 */
	public static boolean notEmpty(Object ob){
		return null!=ob && !"".equals(ob) && !"null".equals(ob);
	}
	
	/**
	 * 2.判断是否“为空”
	 * @param ob
	 * @return
	 */
	public static boolean isEmpty(Object ob){
		return null==ob || "".equals(ob) || "null".equals(ob);
	}
	
	/**
	 * 3.字符串转换为字符串数组
	 * @param str
	 * @param spiltRegex 分隔符
	 * @return
	 */
	public static String[] str2StrArray(String str, String spiltRegex){
		if(isEmpty(str))
			return null;
		return str.split(spiltRegex);
	}
	
	/**
	 * 4.用默认的分隔符(,)将字符串转换为字符串数组
	 * @param str
	 * @return
	 */
	public static String[] str2StrArray(String str){
		return str2StrArray(str, ",\\s*");
	}
	
	/**
	 * 5.按照参数format的格式,日期转字符串
	 * @param date
	 * @param format
	 * @return
	 */
	public static String date2Str(Date date, String format){
		if(notEmpty(date)){
			SimpleDateFormat sdf = new SimpleDateFormat(format);
			return sdf.format(date);
		}else{
			return "";
		}
	}
	
	/**
	 * 6.默认格式"yyyy-MM-dd HH:mm:ss",日期转字符串
	 * @param date
	 * @return
	 */
	public static String date2Str(Date date){
		return date2Str(date, "yyyy-MM-dd HH:mm:ss");
	}
	
	public static Date str2Date(String date){
		if(notEmpty(date)){
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			try {
				return sdf.parse(date);
			} catch (ParseException e) {
				e.printStackTrace();
			}
			return new Date();
		}else{
			return null;
		}
	}
	
	

	
	
}



package com.liam.util;

import org.apache.catalina.core.ApplicationContext;

/**
 * @author liam.huang@foxmail.com
 * 常量工具类
 */
public class Const {
	
	public static final String SESSION_SECURITY_CODE = "sessionSecurityCode";
	public static final String SESSION_USER = "sessionUser";
	public static final String SESSION_USER_RIGHTS = "sessionUserRights";
	public static final String SESSION_ROLE_RIGHTS = "sessionRoleRights";
	//不对匹配该值的访问路径拦截(正则表达式)
	public static final String NO_INTERCEPTOR_PATH = ".*/((login)|(logout)|(code)).*";
	//该值会在web容器启动时由WebAppContextListener初始化
	public static ApplicationContext WEB_APP_CONTEXT = null;

}



package com.liam.util;

import java.io.File;

/**
 * @author liam.huang@foxmail.com
 * 路径获取工具类
 */
public class PathUtil {
	 
	/**
	 * 1.根据路径和文件名,获取文件的路径
	 * @param userPath	"/com/liam/simulator/xml/"
	 * @param filename	"User.xml"
	 * @return
	 */
	public static String getSpecialPath(String userPath, String filename){
		if(BasicUtil.notEmpty(filename))
			return filter(PathUtil.class.getResource(userPath).toString()) + filename;
		else
			return filter(PathUtil.class.getResource(userPath).toString());
	}
	
	/**
	 * 2.在当前路径下,获取文件路径
	 * @return
	 */
	public static String getCurrentPath(String filename){
		if(BasicUtil.notEmpty(filename))
			return filter(PathUtil.class.getResource("").toString()) + filename;
		else
			return filter(PathUtil.class.getResource("").toString());
	}
	
	/**
	 * 3.在src根目录路径下,获取文件路径
	 * @return
	 */
	public static String getSrcRootPath(String filename){
		if(BasicUtil.notEmpty(filename))
			return filter(PathUtil.class.getClassLoader().getResource("").toString()) + filename;
		else
			return filter(PathUtil.class.getClassLoader().getResource("").toString());
	}
	
	/**
	 * 过滤器
	 * @param path
	 * @return
	 */
	public static String filter(String path){
		if(path.startsWith("file"))
			path = path.substring(6);
		path.replace("/", File.separator);
		return path;
	}

}



package com.liam.util;

import java.lang.reflect.Field;

/**
 * @author liam.huang@foxmail.com
 * 反射工具类
 */
public class ReflectUtil {
	
	
	/**
	 * 1.根据对象的filedName获取Field
	 * @param obj
	 * @param fieldName
	 * @return
	 */
	public static Field getFieldByFieldName(Object obj, String fieldName){
		
		for(Class<?> superClass=obj.getClass(); superClass!=Object.class; superClass=superClass.getSuperclass()){
			try {
				return superClass.getDeclaredField(fieldName);
			} catch (NoSuchFieldException e) {
				e.printStackTrace();
			}
		}
		return null;
		
	}
	
	/**
	 * 2.根据对象的filedName获取属性值
	 * @param obj
	 * @param fieldName
	 * @return
	 */
	public static Object getValueByFieldName(Object obj, String fieldName){
		
		Field field = getFieldByFieldName(obj, fieldName);
		Object value = null;
		try {
			if(field!=null){
				if(field.isAccessible()){
						value = field.get(obj);
				} else {
					field.setAccessible(true);
					value = field.get(obj);
					field.setAccessible(false);
				}
			}
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
		return value;
		
	}
	
	/**
	 * 3.根据对象的filedName,设置属性值
	 * @param obj
	 * @param fieldName
	 * @param value
	 */
	public static void setValueByFieldName(Object obj, String fieldName, Object value){
		
		try {
			Field field = obj.getClass().getDeclaredField(fieldName);
			if(field.isAccessible()){
				field.set(obj, value);
			} else {
				field.setAccessible(true);
				field.set(obj, value);
				field.setAccessible(false);
			}
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (NoSuchFieldException e) {
			e.printStackTrace();
		}
	}
	
	
	

}



package com.liam.util;

import java.math.BigInteger;

/**
 * @author liam.huang@foxmail.com
 * 权限核查、计算工具类
 */
public class RightsUtil {
	
	/**
	 * 1.验证是否具有指定编码的权限
	 * @param sum
	 * @param targetRights
	 * @return
	 */
	public static boolean verifyRights(BigInteger sum, int targetRights){
		return sum.testBit(targetRights);
	}
	
	public static boolean verifyRights(String sum, int targetRights){
		if(BasicUtil.isEmpty(sum))
			return false;
		return verifyRights(new BigInteger(sum), targetRights);
	}
	
	public static boolean verifyRights(BigInteger sum, String targetRights){
		return verifyRights(sum,Integer.parseInt(targetRights));
	}
	
	public static boolean verifyRights(String sum, String targetRights){
		if(BasicUtil.isEmpty(sum))
			return false;
		return verifyRights(new BigInteger(sum), Integer.parseInt(targetRights));
	}
	
	
	
	/**
	 * 2.利用BigInteger对权限进行2的权的“和计算”
	 * @param rights
	 * @return
	 */
	public static BigInteger sumRights(String[] rights){
		BigInteger num = new BigInteger("0");
		for(int i=0; i<rights.length; i++)
			num = num.setBit(Integer.parseInt(rights[i]));
		return num;
	}
	
	public static BigInteger sumRights(int[] rights){
		BigInteger num = new BigInteger("0");
		for(int i=0; i<rights.length; i++)
			num = num.setBit(rights[i]);
		return num;
	}
	

}



package com.liam.util;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.font.FontRenderContext;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author liam.huang@foxmail.com
 * 验证码生成工具类
 */
public class VryCodeUtil {
	
	public static String generate(HttpSession session, HttpServletResponse response){
		ByteArrayOutputStream output = new ByteArrayOutputStream();
		String vrfCode = drawImg(output);
		ServletOutputStream out;
		try {
			out = response.getOutputStream();
			output.writeTo(out);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return vrfCode;
	}

	private static String drawImg(ByteArrayOutputStream output) {
		String vrfCode = "";
		for(int i=0; i<4; i++)
			vrfCode += randomChar();
		int width = 70;
		int height = 25;
		BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
		Font font = new Font("Times New Roman", Font.PLAIN, 20);
		Graphics2D g2d = bi.createGraphics();
		g2d.setFont(font);
		Color color = new Color(66,2,82);
		g2d.setColor(color);
		g2d.setBackground(new Color(226,226,240));
		g2d.clearRect(0, 0, width, height);
		FontRenderContext context = g2d.getFontRenderContext();
		Rectangle2D bounds = font.getStringBounds(vrfCode, context);
		double x = (width - bounds.getWidth())/2;
		double y = (height - bounds.getHeight())/2;
		double ascent = bounds.getY();
		double baseY = y - ascent;
		g2d.drawString(vrfCode, (int)x, (int)baseY);
		g2d.dispose();
		try{
			ImageIO.write(bi, "jpg", output);
		}catch(IOException e){
			e.printStackTrace();
		}
		return vrfCode;
	}

	private static char randomChar() {
		Random r = new Random();
		String s = "0123456789";
		return s.charAt(r.nextInt(s.length()));
	}

}




转载于:https://my.oschina.net/lock0818/blog/340735

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值