You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
42 lines
1.5 KiB
42 lines
1.5 KiB
package com.jiudian.manage.until;
|
|
|
|
import javax.imageio.ImageIO;
|
|
import javax.servlet.http.HttpServletResponse;
|
|
import javax.servlet.http.HttpSession;
|
|
import java.awt.*;
|
|
import java.awt.image.BufferedImage;
|
|
import java.io.IOException;
|
|
import java.io.OutputStream;
|
|
import java.util.Random;
|
|
|
|
public class ImageCode {
|
|
public static final String CODENAME="ImageCode";
|
|
public static void createImage(HttpServletResponse response, HttpSession session) throws IOException {
|
|
BufferedImage image = new BufferedImage(80, 30, BufferedImage.TYPE_INT_RGB);
|
|
Graphics g = image.getGraphics();
|
|
Random r = new Random();
|
|
g.setColor(new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255)));
|
|
g.fillRect(0, 0, 80, 20);
|
|
//获取生成的验证码
|
|
String code = getNumber();
|
|
//绑定验证码
|
|
session.setAttribute(CODENAME, code);
|
|
g.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 25));
|
|
g.setColor(new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255)));
|
|
g.drawString(code, 5, 25);
|
|
//设置消息头
|
|
response.setContentType("image/jpeg");
|
|
OutputStream os = response.getOutputStream();
|
|
ImageIO.write(image, "jpeg", os);
|
|
}
|
|
public static String getNumber(){
|
|
String str = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
|
String code = "";
|
|
for(int i= 0;i<4;i++){
|
|
int index = (int)(Math.random()*str.length());
|
|
code+=str.charAt(index);
|
|
}
|
|
return code;
|
|
}
|
|
}
|