標籤:byte Servle char tput 函數 產生 ram tin idt
產生base64格式圖片驗證碼
1 /** 2 * 產生驗證碼 3 * 改造產生驗證碼的方式,將圖片base64形式傳到前台,而不是直接傳驗證碼到前台 4 * @return 5 * @throws IOException 6 */ 7 public void imageCode() throws IOException { 8 HttpServletResponse resp = CommandContext.getResponse(); 9 HttpServletRequest req = CommandContext.getRequest(); 10 String method= req.getMethod(); 11 if("OPTIONS".equals(method)){ 12 return ; 13 } 14 Map map=new HashMap(); 15 16 // 在記憶體中建立圖象 17 int width = 65, height = 38; 18 BufferedImage image = new BufferedImage(width, height, 19 BufferedImage.TYPE_INT_RGB); 20 // 擷取圖形上下文 21 Graphics g = image.getGraphics(); 22 // 產生隨機類 23 Random random = new Random(); 24 // 設定背景色 25 g.setColor(getRandColor(230, 255)); 26 g.fillRect(0, 0, 100, 40); 27 // 設定字型 28 g.setFont(new Font("Arial", Font.CENTER_BASELINE | Font.ITALIC, 20)); 29 // 產生0條幹擾線, 30 g.drawLine(0, 0, 0, 0); 31 32 //存放驗證碼 33 StringBuffer sRand = new StringBuffer(); 34 for (int i = 0; i < charCount; i++) { 35 String singleCode = String.valueOf(codeSequence[random.nextInt(codeSequence.length)]); 36 sRand.append(singleCode); 37 // 將認證碼顯示到圖象中 38 g.setColor(getRandColor(100, 150));// 調用函數出來的顏色相同,可能是因為種子太接近,所以只能直接產生 39 g.drawString(singleCode, 14 * i + 5, 25); 40 } 41 for(int i=0;i<(random.nextInt(5)+5);i++){ 42 g.setColor(new Color(random.nextInt(255)+1,random.nextInt(255)+1,random.nextInt(255)+1)); 43 g.drawLine(random.nextInt(70),random.nextInt(40),random.nextInt(70),random.nextInt(40)); 44 g.drawLine(random.nextInt(70),random.nextInt(40),random.nextInt(70),random.nextInt(40)); 45 } 46 47 HttpSession session = req.getSession(); 48 //擷取clientid 49 String clientId= SystemUtil.getClientId(req); 50 if(StringUtils.isEmpty(clientId)){ 51 //產生clientid 52 String userAgent=req.getHeader("User-Agent"); 53 String sessionId=session.getId(); 54 String cip= IpPolicy.getClientIP(req); 55 clientId=CodeUtil.genClientId(sessionId,cip,userAgent); 56 } 57 map.put("clientId", clientId); 58 if (isValidateCodeCaseSensitive) { 59 session.setAttribute("randomCode", sRand.toString()); 60 SystemUtil.push2Cache(clientId, sRand.toString()); 61 } else { 62 session.setAttribute("randomCode", sRand.toString().toLowerCase()); 63 SystemUtil.push2Cache(clientId, sRand.toString().toLowerCase()); 64 } 65 // 圖象生效 66 g.dispose(); 67 try{ 68 69 ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); 70 ImageIO.write(image, "jpg", outputStream); 71 BASE64Encoder encoder = new BASE64Encoder(); 72 String base64Img = encoder.encode(outputStream.toByteArray()); 73 base64Img="data:image/jpg;base64, "+base64Img.replaceAll("\n", "").replaceAll("\r", "");//刪除 \r\n; 74 map.put("verCode", base64Img); 75 Object jsonObj = JSONSerializer.toJSON(map); 76 byte[] json = jsonObj.toString().getBytes("UTF-8"); 77 resp.setContentType("text/plain;chartset=utf-8"); 78 resp.setHeader("Cache-Control", "no-cache"); 79 resp.setHeader("Expires", "0"); 80 resp.setIntHeader("Content-Length", json.length); 81 ServletOutputStream responseOutputStream = resp.getOutputStream(); 82 responseOutputStream.write(json); 83 // 以下關閉輸入資料流! 84 responseOutputStream.flush(); 85 responseOutputStream.close(); 86 // 獲得頁面key值 87 return ; 88 } catch (IOException e) { 89 logger.error("生產驗證碼出錯",e); 90 throw new SystemException("生產驗證碼出錯",e); 91 } 92 } 93 94 95 /** 96 * 給定範圍獲得隨機顏色 97 * 98 * @param fc 99 * @param bc100 * @return101 */102 Color getRandColor(int fc, int bc) {103 Random random = new Random();104 if (fc > 255)105 fc = 255;106 if (bc > 255)107 bc = 255;108 int r = fc + random.nextInt(bc - fc);109 int g = fc + random.nextInt(bc - fc);110 int b = fc + random.nextInt(bc - fc);111 return new Color(r, g, b);112 }
JAVA產生驗證碼代碼