package com.superscene.wbmsys.util;import java.util.Arrays;public class SecurityCode { public enum SecurityCodeLevel {Simple,Medium,Hard}; public static String getSecurityCode(){ return getSecurityCode(4,SecurityCodeLevel.Medium,false); } public static String getSecurityCode(int length,SecurityCodeLevel level,boolean isCanRepeat){ int len=length; char[] codes={'1','2','3','4','5','6','7','8','9', 'a','b','c','d','e','f','g','h','i','j','k','m','n','p','q','r','s','t','u','v','w','x','y','z', 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','P','Q','R','S','T','U','V','W','X','Y','Z'}; if(level==SecurityCodeLevel.Simple){ codes=Arrays.copyOfRange(codes, 0,9); }else if(level==SecurityCodeLevel.Medium){ codes=Arrays.copyOfRange(codes, 0,33); } int n=codes.length; if(len>n&&isCanRepeat==false){ throw new RuntimeException( String.format("创建验证码失败", len,level,isCanRepeat,n)); } char[] result=new char[len]; if(isCanRepeat){ for(int i=0;i
package com.superscene.wbmsys.util;import java.awt.Color;import java.awt.Font;import java.awt.Graphics;import java.awt.image.BufferedImage;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.util.Random;import javax.imageio.ImageIO;public class SecurityImage { public static BufferedImage createImage(String securityCode, int width, int height){ int codeLength = securityCode.length(); int fSize = 15; BufferedImage image=new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics g=image.createGraphics(); g.setColor(Color.WHITE); g.fillRect(0, 0, width, height); g.setColor(Color.LIGHT_GRAY); g.setFont(new Font("Arial", Font.BOLD, height - 2)); g.drawRect(0, 0, width - 1, height -1); Random rand = new Random(); g.setColor(Color.LIGHT_GRAY); for(int i = 0;i < codeLength * 6;i++){ int x = rand.nextInt(width); int y = rand.nextInt(height); g.drawRect(x, y, 1, 1); } int codeY = height - 6; g.setColor(new Color(19,148,246)); g.setFont(new Font("Georgia", Font.BOLD, fSize)); for(int i = 0; i < codeLength;i++){ g.drawString(String.valueOf(securityCode.charAt(i)), i * 12 + 3, codeY); } g.dispose(); return image; } public static ByteArrayInputStream getImageAsInputStream(String securityCode, int width, int height){ BufferedImage image = createImage(securityCode, width, height); return convertImageToStream(image); } private static ByteArrayInputStream convertImageToStream(BufferedImage image){ ByteArrayInputStream inputStream = null; ByteArrayOutputStream bos = new ByteArrayOutputStream(); try { ImageIO.write(image, "JPEG", bos); byte[] bts = bos.toByteArray(); inputStream = new ByteArrayInputStream(bts); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } return inputStream; }}
@RequestMapping(params="method=getCheckCode",method=RequestMethod.GET) public void getCheckCode(HttpServletRequest request, HttpServletResponse response, @RequestParam("time") String time) { String securityCode = SecurityCode.getSecurityCode(number, SecurityCodeLevel.Hard, false); request.getSession().setAttribute(Constants.USER_LOGIN_CODES, securityCode); BufferedImage image = SecurityImage.createImage(securityCode, width, height); ServletOutputStream out = null; try { out = response.getOutputStream(); // 将图片转换为JPEG类型 //JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); //encoder.encode(image); // 输出图象到页面 ImageIO.write(image, "JPEG", response.getOutputStream()); out.flush(); //ServletActionContext.getPageContext().pushBody(); out.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }