| | |
| | | package com.zy; |
| | | |
| | | import com.zy.common.utils.RandomValidateCodeUtil; |
| | | import org.springframework.boot.SpringApplication; |
| | | import org.springframework.boot.autoconfigure.SpringBootApplication; |
| | | import org.springframework.boot.builder.SpringApplicationBuilder; |
| | |
| | | public class Boot extends SpringBootServletInitializer { |
| | | |
| | | public static void main(String[] args) { |
| | | RandomValidateCodeUtil.init(); |
| | | SpringApplication.run(Boot.class, args); |
| | | } |
| | | |
| | |
| | | return new Color(r, g, b); |
| | | } |
| | | |
| | | public static void init() { |
| | | 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.PLAIN, 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); |
| | | } |
| | | g.dispose(); |
| | | } |
| | | |
| | | /** |
| | | * 生成随机图片 |
| | | */ |
| | |
| | | public void code(@RequestParam String sd, HttpServletResponse response) { |
| | | RandomValidateCodeUtil.getRandcode(sd, response); |
| | | } |
| | | |
| | | @RequestMapping("/code.do") |
| | | public String codeDo(@RequestParam String sd) { |
| | | String code = RandomValidateCodeUtil.code.get(sd); |
| | | public String codeDo(@RequestParam String sd) throws Exception { |
| | | String code = null; |
| | | int time = 0; |
| | | while (time < 3000) { |
| | | code = RandomValidateCodeUtil.code.get(sd); |
| | | if (!Cools.isEmpty(code)){ |
| | | break; |
| | | } else { |
| | | Thread.sleep(10); |
| | | time = time + 100; |
| | | } |
| | | } |
| | | RandomValidateCodeUtil.code.remove(sd); |
| | | return code; |
| | | } |