redis工具类+验证码练习

package com.tarena.platform.cache.util;


import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;


/**
 * 缓存工具类
 * @author chen
 *
 */
@SuppressWarnings("unchecked")
public class CacheUtil {


    private static final Logger LOG = LoggerFactory.getLogger(CacheUtil.class);


    private static RedisTemplate<String, Object> redisTemplate = CacheContextUtil.getBean("platformRedisTemplate", RedisTemplate.class);


    private static StringRedisTemplate stringRedisTemplate = CacheContextUtil.getBean("platformStringRedisTemplate", StringRedisTemplate.class);


    private static String CACHE_PREFIX;


    private static boolean CACHE_CLOSED;


    private CacheUtil() {


    }


    @SuppressWarnings("rawtypes")
    private static boolean isEmpty(Object obj) {
if (obj == null) {
   return true;
}
if (obj instanceof String) {
   String str = obj.toString();
   if ("".equals(str.trim())) {
return true;
   }
   return false;
}
if (obj instanceof List) {
   List<Object> list = (List<Object>) obj;
   if (list.isEmpty()) {
return true;
   }
   return false;
}
if (obj instanceof Map) {
   Map map = (Map) obj;
   if (map.isEmpty()) {
return true;
   }
   return false;
}
if (obj instanceof Set) {
   Set set = (Set) obj;
   if (set.isEmpty()) {
return true;
   }
   return false;
}
if (obj instanceof Object[]) {
   Object[] objs = (Object[]) obj;
   if (objs.length <= 0) {
return true;
   }
   return false;
}
return false;
    }


    /**
     * 构建缓存key值
     * @param key 缓存key
     * @return
     */
    private static String buildKey(String key) {
if (CACHE_PREFIX == null || "".equals(CACHE_PREFIX)) {
   return key;
}
return CACHE_PREFIX + ":" + key;
    }


    /**
     * 返回缓存的前缀
     * @return CACHE_PREFIX_FLAG
     */
    public static String getCachePrefix() {
return CACHE_PREFIX;
    }


    /**
     * 设置缓存的前缀
     * @param cachePrefix
     */
    public static void setCachePrefix(String cachePrefix) {
if (cachePrefix != null) {
   CACHE_PREFIX = cachePrefix.trim();
}
    }


    /**
     * 关闭缓存
     * @return true:成功 
     * false:失败
     */
    public static boolean close() {
LOG.debug(" cache closed ! ");
CACHE_CLOSED = true;
return true;
    }


    /**
     * 打开缓存
     * @return true:存在
     * false:不存在
     */
    public static boolean openCache() {
CACHE_CLOSED = false;
return true;
    }


    /**
     * 检查缓存是否开启
     * @return true:已关闭 
     * false:已开启
     */
    public static boolean isClose() {
return CACHE_CLOSED;
    }


    /**
     * 判断key值是否存在
     * @param key 缓存的key
     * @return true:存在
     * false:不存在
     */
    public static boolean hasKey(String key) {
LOG.debug(" hasKey key :{}", key);
try {
   if (isClose() || isEmpty(key)) {
return false;
   }
   key = buildKey(key);
   return redisTemplate.hasKey(key);
} catch (Exception e) {
   LOG.error(e.getMessage(), e);
}
return false;
    }


    /**
     * 匹配符合正则的key
     * @param patternKey
     * @return key的集合
     */
    public static Set<String> keys(String patternKey) {
LOG.debug(" keys key :{}", patternKey);
try {
   if (isClose() || isEmpty(patternKey)) {
return Collections.emptySet();
   }
   return redisTemplate.keys(patternKey);
} catch (Exception e) {
   LOG.error(e.getMessage(), e);
}
return Collections.emptySet();
    }


    /**
     * 根据key删除缓存
     * @param key
     * @return true:成功 
     * false:失败
     */
    public static boolean del(String... key) {
LOG.debug(" delete key :{}", key.toString());
try {
   if (isClose() || isEmpty(key)) {
return false;
   }
   Set<String> keySet = new HashSet<>();
   for (String str : key) {
keySet.add(buildKey(str));
   }
   redisTemplate.delete(keySet);
   return true;
} catch (Exception e) {
   LOG.error(e.getMessage(), e);
}
return false;
    }


    /**
     * 根据key删除缓存
     * @param key
     * @return true:成功 
     * false:失败
     */
    public static boolean delPattern(String key) {
LOG.debug(" delete Pattern keys :{}", key);
try {
   if (isClose() || isEmpty(key)) {
return false;
   }
   key = buildKey(key);
   redisTemplate.delete(redisTemplate.keys(key));
   return true;
} catch (Exception e) {
   LOG.error(e.getMessage(), e);
}
return false;
    }


    /**
     * 删除一组key值
     * @param keys
     * @return true:成功 
     * false:失败
     */
    public static boolean del(Set<String> keys) {
LOG.debug(" delete keys :{}", keys.toString());
try {
   if (isClose() || isEmpty(keys)) {
return false;
   }
   Set<String> keySet = new HashSet<>();
   for (String str : keys) {
keySet.add(buildKey(str));
   }
   redisTemplate.delete(keySet);
   return true;
} catch (Exception e) {
   LOG.error(e.getMessage(), e);
}
return false;
    }


    /**
     * 设置过期时间
     * @param key 缓存key
     * @param seconds 过期秒数
     * @return true:成功 
     * false:失败
     */
    public static boolean setExp(String key, long seconds) {
LOG.debug(" setExp key :{}, seconds: {}", key, seconds);
try {
   if (isClose() || isEmpty(key) || seconds > 0) {
return false;
   }
   key = buildKey(key);
   return redisTemplate.expire(key, seconds, TimeUnit.SECONDS);
} catch (Exception e) {
   LOG.error(e.getMessage(), e);
}
return false;
    }


    /**
     * 查询过期时间
     * @param key 缓存key
     * @return 秒数
     */
    public static Long getExpire(String key) {
LOG.debug(" getExpire key :{}", key);
try {
   if (isClose() || isEmpty(key)) {
return 0L;
   }
   key = buildKey(key);
   return redisTemplate.getExpire(key, TimeUnit.SECONDS);
} catch (Exception e) {
   LOG.error(e.getMessage(), e);
}
return 0L;
    }
    
    /**
     * 缓存存入key-value
     * @param key 缓存键
     * @param value 缓存值
     * @return true:成功 
     * false:失败
     */
    public static boolean setString(String key, String value) {
LOG.debug(" setString key :{}, value: {}", key, value);
try {
   if (isClose() || isEmpty(key) || isEmpty(value)) {
return false;
   }
   key = buildKey(key);
   stringRedisTemplate.opsForValue().set(key, value);
   return true;
} catch (Exception e) {
   LOG.error(e.getMessage(), e);
}
return false;
    }


    /**
     * 缓存存入key-value
     * @param key 缓存键
     * @param value 缓存值
     * @param seconds 秒数
     * @return true:成功 
     * false:失败
     */
    public static boolean setString(String key, String value, long seconds) {
LOG.debug(" setString key :{}, value: {}, timeout:{}", key, value, seconds);
try {
   if (isClose() || isEmpty(key) || isEmpty(value)) {
return false;
   }
   key = buildKey(key);
   stringRedisTemplate.opsForValue().set(key, value, seconds, TimeUnit.SECONDS);
   return true;
} catch (Exception e) {
   LOG.error(e.getMessage(), e);
}
return false;
    }


    /**
     * 根据key取出String value
     * @param key 缓存key值
     * @return String 缓存的String
     */
    public static String getString(String key) {
LOG.debug(" getString key :{}", key);
try {
   if (isClose() || isEmpty(key)) {
return null;
   }
   key = buildKey(key);
   return stringRedisTemplate.opsForValue().get(key);
} catch (Exception e) {
   LOG.error(e.getMessage(), e);
}
return null;
    }


    /**
     * 去的缓存中的最大值并+1
     * @param key 缓存key值
     * @return long 缓存中的最大值+1
     */
    public static long incr(String key) {
LOG.debug(" incr key :{}", key);
try {
   if (isClose() || isEmpty(key)) {
return 0;
   }
   key = buildKey(key);
   return redisTemplate.opsForValue().increment(key, 1);
} catch (Exception e) {
   LOG.error(e.getMessage(), e);
}
return 0;
    }


    /**
     * 缓存中存入序列化的Object对象
     * @param <T>
     * @param key 缓存key
     * @param obj 存入的序列化对象
     * @return true:成功 
     * false:失败
     */
    public static boolean set(String key, Object obj) {
LOG.debug(" set key :{}, value:{}", key, obj);
try {
   if (isClose() || isEmpty(key) || isEmpty(obj)) {
return false;
   }
   key = buildKey(key);
   redisTemplate.opsForValue().set(key, obj);
} catch (Exception e) {
   LOG.error(e.getMessage(), e);
}
return false;
    }


    /**
     * 缓存中存入序列化的Object对象
     * @param <T>
     * @param key 缓存key
     * @param obj 存入的序列化对象
     * @return true:成功 
     * false:失败
     */
    public static boolean setObj(String key, Object obj, long seconds) {
LOG.debug(" set key :{}, value:{}, seconds:{}", key, obj, seconds);
try {
   if (isClose() || isEmpty(key) || isEmpty(obj)) {
return false;
   }
   key = buildKey(key);
   redisTemplate.opsForValue().set(key, obj);
   if (seconds > 0) {
redisTemplate.expire(key, seconds, TimeUnit.SECONDS);
   }
   return true;
} catch (Exception e) {
   LOG.error(e.getMessage(), e);
}
return false;
    }


    /**
     * 取出缓存中存储的序列化对象
     * @param key 缓存key
     * @param clazz 对象类
     * @return <T> 序列化对象
     */
    public static <T> T getObj(String key, Class<T> clazz) {
LOG.debug(" get key :{}", key);
try {
   if (isClose() || isEmpty(key)) {
return null;
   }
   key = buildKey(key);
   return (T) redisTemplate.opsForValue().get(key);
} catch (Exception e) {
   LOG.error(e.getMessage(), e);
}
return null;
    }


    /**
     * 存入Map数组
     * @param <T>
     * @param key 缓存key
     * @param map 缓存map
     * @return true:成功 
     * false:失败
     */
    public static <T> boolean setMap(String key, Map<String, T> map) {
try {
   if (isClose() || isEmpty(key) || isEmpty(map)) {
return false;
   }
   key = buildKey(key);
   redisTemplate.opsForHash().putAll(key, map);
   return true;
} catch (Exception e) {
   LOG.error(e.getMessage(), e);
}
return false;
    }


    /**
     * 取出缓存的map
     * @param key 缓存key
     * @return map 缓存的map
     */
    @SuppressWarnings("rawtypes")
    public static Map getMap(String key) {
LOG.debug(" getMap key :{}", key);
try {
   if (isClose() || isEmpty(key)) {
return null;
   }
   key = buildKey(key);
   return redisTemplate.opsForHash().entries(key);
} catch (Exception e) {
   LOG.error(e.getMessage(), e);
}
return null;
    }


    /**
     * 根据key以及hashKey取出对应的Object对象
     * @param key 缓存key
     * @param hashKey 对应map的key
     * @return object map中的对象
     */
    public static Object getMapKey(String key, String hashKey) {
LOG.debug(" getMapkey :{}, hashKey:{}", key, hashKey);
try {
   if (isClose() || isEmpty(key) || isEmpty(hashKey)) {
return null;
   }
   key = buildKey(key);
   return redisTemplate.opsForHash().get(key, hashKey);
} catch (Exception e) {
   LOG.error(e.getMessage(), e);
}
return null;
    }


    /**
     * 取出缓存中map的所有key值
     * @param key 缓存key
     * @return Set<String> map的key值合集
     */
    public static Set<Object> getMapKeys(String key) {
LOG.debug(" getMapKeys key :{}", key);
try {
   if (isClose() || isEmpty(key)) {
return null;
   }
   key = buildKey(key);
   return redisTemplate.opsForHash().keys(key);
} catch (Exception e) {
   LOG.error(e.getMessage(), e);
}
return null;
    }


    /**
     * 删除map中指定的key值
     * @param key 缓存key
     * @param hashKey map中指定的hashKey
     * @return true:成功 
     * false:失败
     */
    public static boolean delMapKey(String key, String hashKey) {
LOG.debug(" delMapKey key :{}, hashKey:{}", key, hashKey);
try {
   if (isClose() || isEmpty(key) || isEmpty(hashKey)) {
return false;
   }
   key = buildKey(key);
   redisTemplate.opsForHash().delete(key, hashKey);
   return true;
} catch (Exception e) {
   LOG.error(e.getMessage(), e);
}
return false;
    }


    /**
     * 存入Map数组
     * @param <T>
     * @param key 缓存key
     * @param map 缓存map
     * @param seconds 秒数
     * @return true:成功 
     * false:失败
     */
    public static <T> boolean setMapExp(String key, Map<String, T> map, long seconds) {
LOG.debug(" setMapExp key :{}, value: {}, seconds:{}", key, map, seconds);
try {
   if (isClose() || isEmpty(key) || isEmpty(map)) {
return false;
   }
   key = buildKey(key);
   redisTemplate.opsForHash().putAll(key, map);
   redisTemplate.expire(key, seconds, TimeUnit.SECONDS);
   return true;
} catch (Exception e) {
   LOG.error(e.getMessage(), e);
}
return false;
    }


    /**
     * map中加入新的key
     * @param <T>
     * @param key 缓存key
     * @param hashKey map的Key值
     * @param value map的value值
     * @return true:成功 
     * false:失败
     */
    public static <T> boolean addMap(String key, String hashKey, T value) {
LOG.debug(" addMap key :{}, hashKey: {}, value:{}", key, hashKey, value);
try {
   if (isClose() || isEmpty(key) || isEmpty(hashKey) || isEmpty(value)) {
return false;
   }
   key = buildKey(key);
   redisTemplate.opsForHash().put(key, hashKey, value);
   return true;
} catch (Exception e) {
   LOG.error(e.getMessage(), e);
}
return false;
    }


    /**
     * 缓存存入List
     * @param <T>
     * @param key 缓存key
     * @param list 缓存List
     * @return true:成功 
     * false:失败
     */
    public static <T> boolean setList(String key, List<T> list) {
LOG.debug(" setList key :{}, list: {}", key, list);
try {
   if (isClose() || isEmpty(key) || isEmpty(list)) {
return false;
   }
   key = buildKey(key);
   redisTemplate.opsForList().leftPushAll(key, list.toArray());
} catch (Exception e) {
   LOG.error(e.getMessage(), e);
}
return false;
    }


    /**
     * 根据key值取出对应的list合集
     * @param key 缓存key
     * @return List<Object> 缓存中对应的list合集
     */
    public static <V> List<V> getList(String key) {
LOG.debug(" getList key :{}", key);
try {
   if (isClose() || isEmpty(key)) {
return null;
   }
   key = buildKey(key);
   return (List<V>) redisTemplate.opsForList().range(key, 0, -1);
} catch (Exception e) {
   LOG.error(e.getMessage(), e);
}
return null;
    }


    /**
     * 根据key值截取对应的list合集
     * @param key 缓存key
     * @param start 开始位置
     * @param end 结束位置
     * @return
     */
    public static void trimList(String key, int start, int end) {
LOG.debug(" trimList key :{}", key);
try {
   if (isClose() || isEmpty(key)) {
return;
   }
   key = buildKey(key);
   redisTemplate.opsForList().trim(key, start, end);
} catch (Exception e) {
   LOG.error(e.getMessage(), e);
}
    }
    
    /**
     * 取出list合集中指定位置的对象
     * @param key 缓存key
     * @param index 索引位置
     * @return Object list指定索引位置的对象
     */
    public static Object getIndexList(String key, int index) {
LOG.debug(" getIndexList key :{}, index:{}", key, index);
try {
   if (isClose() || isEmpty(key) || index < 0) {
return null;
   }
   key = buildKey(key);
   return redisTemplate.opsForList().index(key, index);
} catch (Exception e) {
   LOG.error(e.getMessage(), e);
}
return null;
    }


    /**
     * Object存入List
     * @param <T>
     * @param key 缓存key
     * @param value List中的值
     * @return true:成功 
     * false:失败
     */
    public static boolean addList(String key, Object value) {
LOG.debug(" addList key :{}, value:{}", key, value);
try {
   if (isClose() || isEmpty(key) || isEmpty(value)) {
return false;
   }
   key = buildKey(key);
   redisTemplate.opsForList().leftPush(key, value);
   return true;
} catch (Exception e) {
   LOG.error(e.getMessage(), e);
}
return false;
    }


    /**
     * 缓存存入List
     * @param <T>
     * @param key 缓存key
     * @param list 缓存List
     * @param seconds 秒数
     * @return true:成功 
     * false:失败
     */
    public static <T> boolean setList(String key, List<T> list, long seconds) {
LOG.debug(" setList key :{}, value:{}, seconds:{}", key, list, seconds);
try {
   if (isClose() || isEmpty(key) || isEmpty(list)) {
return false;
   }
   key = buildKey(key);
   redisTemplate.opsForList().leftPushAll(key, list.toArray());
   if (seconds > 0) {
redisTemplate.expire(key, seconds, TimeUnit.SECONDS);
   }
   return true;
} catch (Exception e) {
   LOG.error(e.getMessage(), e);
}
return false;
    }


    /**
     * set集合存入缓存
     * @param <T>
     * @param key 缓存key
     * @param set 缓存set集合
     * @return true:成功 
     * false:失败
     */
    public static <T> boolean setSet(String key, Set<T> set) {
LOG.debug(" setSet key :{}, value:{}", key, set);
try {
   if (isClose() || isEmpty(key) || isEmpty(set)) {
return false;
   }
   key = buildKey(key);
   redisTemplate.opsForSet().add(key, set.toArray());
   return true;
} catch (Exception e) {
   LOG.error(e.getMessage(), e);
}
return false;
    }


    /**
     * set集合中增加value
     * @param <T>
     * @param key 缓存key
     * @param value 增加的value
     * @return true:成功 
     * false:失败
     */
    public static boolean addSet(String key, Object value) {
LOG.debug(" addSet key :{}, value:{}", key, value);
try {
   if (isClose() || isEmpty(key) || isEmpty(value)) {
return false;
   }
   key = buildKey(key);
   redisTemplate.opsForSet().add(key, value);
   return true;
} catch (Exception e) {
   LOG.error(e.getMessage(), e);
}
return false;
    }


    /**
     * set集合存入缓存
     * @param <T>
     * @param key 缓存key
     * @param set 缓存set集合
     * @param seconds 秒数
     * @return true:成功 
     * false:失败
     */
    public static <T> boolean setSet(String key, Set<T> set, long seconds) {
LOG.debug(" setSet key :{}, value:{}, seconds:{}", key, set, seconds);
try {
   if (isClose() || isEmpty(key) || isEmpty(set)) {
return false;
   }
   key = buildKey(key);
   redisTemplate.opsForSet().add(key, set.toArray());
   if (seconds > 0) {
redisTemplate.expire(key, seconds, TimeUnit.SECONDS);
   }
   return true;
} catch (Exception e) {
   LOG.error(e.getMessage(), e);
}
return false;
    }


    /**
     * 取出缓存中对应的set合集
     * @param <T>
     * @param key 缓存key
     * @return Set<Object> 缓存中的set合集
     */
    public static <T> Set<T> getSet(String key) {
LOG.debug(" getSet key :{}", key);
try {
   if (isClose() || isEmpty(key)) {
return null;
   }
   key = buildKey(key);
   return (Set<T>) redisTemplate.opsForSet().members(key);
} catch (Exception e) {
   LOG.error(e.getMessage(), e);
}
return null;
    }


    /**
     * 有序集合存入数值
     * @param key 缓存key
     * @param value 缓存value
     * @param score 评分
     * @return
     */
    public static boolean addZSet(String key, Object value, double score) {
LOG.debug(" addZSet key :{},value:{}, score:{}", key, value, score);
try {
   if (isClose() || isEmpty(key) || isEmpty(value)) {
return false;
   }
   key = buildKey(key);
   return redisTemplate.opsForZSet().add(key, value, score);
} catch (Exception e) {
   LOG.error(e.getMessage(), e);
}
return false;
    }


    /**
     * 从有序集合中删除指定值
     * @param key 缓存key
     * @param value 缓存value
     * @return
     */
    public static boolean removeZSet(String key, Object value) {
LOG.debug(" removeZSet key :{},value:{}", key, value);
try {
   if (isClose() || isEmpty(key) || isEmpty(value)) {
return false;
   }
   key = buildKey(key);
   redisTemplate.opsForZSet().remove(key, value);
   return true;
} catch (Exception e) {
   LOG.error(e.getMessage(), e);
}
return false;
    }


    /**
     * 从有序集合中删除指定位置的值
     * @param key 缓存key
     * @param start 起始位置
     * @param end 结束为止
     * @return
     */
    public static boolean removeZSet(String key, long start, long end) {
LOG.debug(" removeZSet key :{},start:{}, end:{}", key, start, end);
try {
   if (isClose() || isEmpty(key)) {
return false;
   }
   key = buildKey(key);
   redisTemplate.opsForZSet().removeRange(key, start, end);
   return true;
} catch (Exception e) {
   LOG.error(e.getMessage(), e);
}
return false;
    }


    /**
     * 从有序集合中获取指定位置的值
     * @param key 缓存key
     * @param start 起始位置
     * @param end 结束为止
     * @return
     */
    public static <T> Set<T> getZSet(String key, long start, long end) {
LOG.debug(" getZSet key :{},start:{}, end:{}", key, start, end);
try {
   if (isClose() || isEmpty(key)) {
return Collections.emptySet();
   }
   key = buildKey(key);
   return (Set<T>) redisTemplate.opsForZSet().range(key, start, end);
} catch (Exception e) {
   LOG.error(e.getMessage(), e);
}
return Collections.emptySet();
    }

}

2、验证码练习:

package com.tarena.tbsfront.util;


import java.awt.Color;


import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.Date;
import java.util.Random;


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


import com.tarena.platform.cache.util.CacheUtil;


public class RandomValidateCode {
    static String E_valcode="E-CN-";//--要提出去
    static String RANDOMCODEKEY="T_RANDOM_IMG_KEY";//--要提出去
    
    private static Random random = new Random();
    private static String randString = "234567ACDEFGHJKMNPQRSTUVWXYZ";//随机产生的字符串
    
    private static int width = 80;//图片宽
    private static int height = 26;//图片高
    private static int lineSize = 50;//干扰线数量
    private static int stringNum = 4;//随机产生字符数量
    
    public static String getValidateCode(HttpSession session) {
    return (String)session.getAttribute(RANDOMCODEKEY);//--手写的要提出去
    }
    
    public static boolean checkValidateCode(String yourInput, String uuid){
    //String valid = (String)session.getAttribute(SessionUtil.RANDOMCODEKEY);
try {
String valid = CacheUtil.getString( E_valcode+uuid );//--手写的要提出去
if (valid !=null && yourInput != null && !yourInput.equals("") && yourInput.toUpperCase().equals(valid)) {
System.out.println("\n"+new Date()+"< GET Valid CODE > : " + uuid + "="+valid + "\n yourInput = " + yourInput);//TODO delete sys.out
    return true;
    }
    return false;
} catch (Exception e) {
e.printStackTrace();
return true;
}
    }
    
    /**
     * 生成随机图片
     */
    public static void generateValidateCode(HttpServletRequest request, HttpServletResponse response) {
    //HttpSession session = request.getSession();
        //BufferedImage类是具有缓冲区的Image类,Image类是用于描述图像信息的类
        BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_BGR);
        Graphics g = image.getGraphics();//产生Image对象的Graphics对象,改对象可以在图像上进行各种绘制操作
        g.fillRect(0, 0, width, height);
        g.setFont(new Font("Times New Roman",Font.ROMAN_BASELINE,18));
        g.setColor(getRandColor(110, 133));
        //绘制干扰线
        for(int i=0;i<=lineSize;i++){
            drowLine(g);
        }
        //绘制随机字符
        String randomString = "";
        for(int i=1;i<=stringNum;i++){
            randomString=drowString(g,randomString,i);
        }
        //session.removeAttribute(SessionUtil.RANDOMCODEKEY);
        //session.setAttribute(SessionUtil.RANDOMCODEKEY, randomString);
        try {
        String uuid = request.getParameter("uuid");
        System.out.println("\n"+new Date()+"< GET Valid CODE > : " + uuid + "="+randomString + "\n");//TODO delete sys.out
        CacheUtil.setString( E_valcode+uuid, randomString, 10*60 );//单位:秒
} catch (Exception e) {
e.printStackTrace();
}
        g.dispose();
        try {
            ImageIO.write(image, "JPEG", response.getOutputStream());//将内存中的图片通过流动形式输出到客户端
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    /*
     * 获得字体
     */
    private static Font getFont(){
        return new Font("Fixedsys",Font.CENTER_BASELINE,18);
    }
    /*
     * 获得颜色
     */
    private static Color getRandColor(int fc,int bc){
        if(fc > 255)
            fc = 255;
        if(bc > 255)
            bc = 255;
        int r = fc + random.nextInt(bc-fc-16);
        int g = fc + random.nextInt(bc-fc-14);
        int b = fc + random.nextInt(bc-fc-18);
        return new Color(r,g,b);
    }
    
    /*
     * 绘制字符串
     */
    private static String drowString(Graphics g,String randomString,int i){
        g.setFont(getFont());
        g.setColor(new Color(random.nextInt(101),random.nextInt(111),random.nextInt(121)));
        String rand = String.valueOf(getRandomString(random.nextInt(randString.length())));
        randomString +=rand;
        g.translate(random.nextInt(3), random.nextInt(3));
        g.drawString(rand, 13*i, 16);
        return randomString;
    }
    /*
     * 绘制干扰线
     */
    private static void drowLine(Graphics g){
        int x = random.nextInt(width);
        int y = random.nextInt(height);
        int xl = random.nextInt(13);
        int yl = random.nextInt(15);
        g.drawLine(x, y, x+xl, y+yl);
    }
    /*
     * 获取随机的字符
     */
    public static String getRandomString(int num){
        return String.valueOf(randString.charAt(num));
    }
    public static String getRandomNum(int num){
    String str = "";
    for(int i=0;i<num;i++){
    str += random.nextInt(10);
    }
    return str;
    }
}


3、调用该方法

/**生成验证码
     * @param response
     * @param request
     */
    @RequestMapping("/validateCode")
    public void validateCode(HttpServletResponse response,
HttpServletRequest request) {
    RandomValidateCode.generateValidateCode(request, response);
    }




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值