java登录的 验证码_java登录验证码
一、創(chuàng)建web項(xiàng)目
二、生成驗(yàn)證碼
創(chuàng)建VcodeObject.java,存儲(chǔ)驗(yàn)證碼與圖片
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
}
創(chuàng)建Vcode.java生成驗(yàn)證碼與圖片
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() {
// 生成隨機(jī)驗(yàn)證碼
String code = this.generateRandomVerifyCode(4);
// 生成附帶驗(yàn)證碼的圖片
BufferedImage img = this.generateImgWithCode(code, 80, 34);
InputStream in = this.putImgInStream(img);
return new VcodeObject(code, in);
}
/**
* 隨機(jī)生成驗(yàn)證碼
*
* @param length
* @return String
*/
private String generateRandomVerifyCode(int length) {
// 需要生成驗(yàn)證碼的長(zhǎng)度
if (length <= 0 || length >= 8)
length = 4;
// 隨機(jī)生成字符的范圍
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();
}
/**
* 生成帶有驗(yàn)證碼的圖片
*
* @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();
// 設(shè)置背景和邊框顏色
brush.setColor(this.getColor());
brush.fillRect(0, 0, width - 1, height - 1);
brush.drawRect(0, 0, width - 1, height - 1);
// 寫入驗(yàn)證碼
brush.setColor(this.getColor());
brush.setFont(new Font("Times New Roman", Font.ITALIC, 28));
brush.drawString(code, 7, 22);
// 繪制干擾線條
// 圖片畫完,關(guān)閉資源
brush.dispose();
return img;
}
/**
* 獲取隨機(jī)顏色
*
* @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;
}
}
三、輸出驗(yàn)證碼
創(chuàng)建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
瀏覽器地址欄訪問(wèn)
http://127.0.0.1:8080/vcode/vcode
總結(jié)
以上是生活随笔為你收集整理的java登录的 验证码_java登录验证码的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: java里的字符流_javaIO流中字符
- 下一篇: 阿克斯java表_java 入门第三季的