一、创建web项目
二、生成验证码
创建VcodeObject.java,存储验证码与图片
package com.demo.vcode;
import java.io.InputStream;
public class VcodeObject {
private String code;
private InputStream in;
public VcodeObject() {
}
public VcodeObject(String code, InputStream in) {
this.code = code;
this.in = in;
}
//getters & setters
}
创建Vcode.java生成验证码与图片
package com.demo.vcode;
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.io.InputStream;
import java.util.Random;
import javax.imageio.ImageIO;
public class Vcode {
public VcodeObject create() {
// 生成随机验证码
String code = this.generateRandomVerifyCode(4);
// 生成附带验证码的图片
BufferedImage img = this.generateImgWithCode(code, 80, 34);
InputStream in = this.putImgInStream(img);
return new VcodeObject(code, in);
}
/**
* 随机生成验证码
*
* @param length
* @return String
*/
private String generateRandomVerifyCode(int length) {
// 需要生成验证码的长度
if (length <= 0 || length >= 8)
length = 4;
// 随机生成字符的范围
String scope = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
StringBuffer code = new StringBuffer();
Random random = new Random();
for (int i = 0; i < length; i++) {
code.append(scope.charAt(random.nextInt(62)));
}
return code.toString();
}
/**
* 生成带有验证码的图片
*
* @param code
* @param width
* @param height
* @return BufferedImage
*/
private BufferedImage generateImgWithCode(String code, int width, int height) {
BufferedImage img = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics brush = img.getGraphics();
// 设置背景和边框颜色
brush.setColor(this.getColor());
brush.fillRect(0, 0, width - 1, height - 1);
brush.drawRect(0, 0, width - 1, height - 1);
// 写入验证码
brush.setColor(this.getColor());
brush.setFont(new Font("Times New Roman", Font.ITALIC, 28));
brush.drawString(code, 7, 22);
// 绘制干扰线条
// 图片画完,关闭资源
brush.dispose();
return img;
}
/**
* 获取随机颜色
*
* @return Color
*/
private Color getColor() {
Random random = new Random();
int r = random.nextInt(255);
int g = random.nextInt(255);
int b = random.nextInt(255);
return new Color(r, g, b);
}
/**
* 将图片放入输入流中
*
* @param image
* @return InputStream
*/
private InputStream putImgInStream(BufferedImage image) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
ImageIO.write(image, "JPEG", out);
} catch (IOException e) {
e.printStackTrace();
}
InputStream in = new ByteArrayInputStream(out.toByteArray());
return in;
}
}
三、输出验证码
创建DemoServlet.java
package com.demo.vcode;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class DemoServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public void init(ServletConfig config) throws ServletException {
}
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws IOException, ServletException {
res.setContentType("image/png");
Vcode vcode = new Vcode();
VcodeObject vcodeObject = vcode.create();
InputStream in = vcodeObject.getIn();
req.getSession().setAttribute("vcode", vcodeObject.getCode());
byte[] bytes = new byte[1024];
int length = 0;
try {
OutputStream out = res.getOutputStream();
while ((length = in.read(bytes)) != -1) {
out.write(bytes, 0, length);
}
in.close();
out.flush();
} catch (Exception e) {
}
}
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws IOException, ServletException {
}
public void destroy() {
}
}
配置web.xml
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
vcode
vcode
com.demo.vcode.DemoServlet
vcode
/vcode
浏览器地址栏访问
http://127.0.0.1:8080/vcode/vcode