谁有Java验证码的Servlet啊?
- 提问者网友:星軌
- 2021-07-31 23:54
- 五星知识达人网友:千夜
- 2021-08-01 01:18
我有,发给你哈,你要改下报名。和类名。
package com.vanceinfo.web.servlet;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import com.vanceinfo.common.util.RandomChar;
public class AuthImg extends HttpServlet {
private static final long serialVersionUID = -8281845659081613239L;
private Font mFont = new Font("Times New Roman", Font.PLAIN, 18);
public void init() throws ServletException {
}
// Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("image/jpeg");
ServletOutputStream out = response.getOutputStream();
int width = 60, height = 20;
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
Random random = new Random();
g.setColor(getRandColor(200, 250));
g.fillRect(0, 0, width, height);
g.setFont(mFont);
g.setColor(getRandColor(160, 200));
for (int i = 0; i < 155; i++) {
int x = random.nextInt(width);
int y = random.nextInt(height);
int xl = random.nextInt(12);
int yl = random.nextInt(12);
g.drawLine(x, y, x + xl, y + yl);
}
String rand = RandomChar.getChars(2, 4);
char c;
for (int i = 0; i < 4; i++) {
c = rand.charAt(i);
g.setColor(new Color(20 + random.nextInt(110), 20 + random
.nextInt(110), 20 + random.nextInt(110))); // ���ú����4����ɫ��ͬ����������Ϊ����̫�ӽ�����ֻ��ֱ�����
g.drawString(String.valueOf(c), 13 * i + 6, 16);
}
HttpSession seesion = request.getSession();
seesion.setAttribute("authCode", rand);
String auth=(String)seesion.getAttribute("authCode");
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(image);
out.close();
}
// Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
// Clean up resources
public void destroy() {
}
private Color getRandColor(int fc, int bc) { // ��Χ��������ɫ
Random random = new Random();
if (fc > 255) {
fc = 255;
}
if (bc > 255) {
bc = 255;
}
int r = fc + random.nextInt(bc - fc);
int g = fc + random.nextInt(bc - fc);
int b = fc + random.nextInt(bc - fc);
return new Color(r, g, b);
}
}
- 1楼网友:長槍戰八方
- 2021-08-01 02:28
http://user.qzone.qq.com/549726411/infocenter?ptlang=2052
去我空间看看把 写的很详细 希望能帮助你
- 2楼网友:鱼忧
- 2021-08-01 01:52
import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import java.awt.*; import java.awt.image.*; import java.util.*; import javax.imageio.*;
public class AuthImg extends HttpServlet { private Font mFont = new Font("Arial Black", Font.PLAIN, 16); public void init() throws ServletException { super.init(); } Color getRandColor(int fc,int bc) { Random random = new Random(); if(fc>255) fc=255; if(bc>255) bc=255; int r=fc+random.nextInt(bc-fc); int g=fc+random.nextInt(bc-fc); int b=fc+random.nextInt(bc-fc); return new Color(r,g,b); }
public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setHeader("Pragma","No-cache"); response.setHeader("Cache-Control","no-cache"); response.setDateHeader("Expires", 0); response.setContentType("image/jpeg"); int width=100, height=18; BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics g = image.getGraphics(); Random random = new Random(); g.setColor(getRandColor(200,250)); g.fillRect(1, 1, width-1, height-1); g.setColor(new Color(102,102,102)); g.drawRect(0, 0, width-1, height-1); g.setFont(mFont);
g.setColor(getRandColor(160,200)); for (int i=0;i<155;i++) { int x = random.nextInt(width - 1); int y = random.nextInt(height - 1); int xl = random.nextInt(6) + 1; int yl = random.nextInt(12) + 1; g.drawLine(x,y,x + xl,y + yl); } for (int i = 0;i < 70;i++) { int x = random.nextInt(width - 1); int y = random.nextInt(height - 1); int xl = random.nextInt(12) + 1; int yl = random.nextInt(6) + 1; g.drawLine(x,y,x - xl,y - yl); }
String sRand=""; for (int i=0;i<6;i++) { String tmp = getRandomChar(); sRand += tmp; g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110))); g.drawString(tmp,15*i+10,15); }
HttpSession session = request.getSession(true); session.setAttribute("rand",sRand); g.dispose(); ImageIO.write(image, "JPEG", response.getOutputStream()); } private String getRandomChar() { int rand = (int)Math.round(Math.random() * 2); long itmp = 0; char ctmp = '\u0000'; switch (rand) { case 1: itmp = Math.round(Math.random() * 25 + 65); ctmp = (char)itmp; return String.valueOf(ctmp); case 2: itmp = Math.round(Math.random() * 25 + 97); ctmp = (char)itmp; return String.valueOf(ctmp); default : itmp = Math.round(Math.random() * 9); return String.valueOf(itmp); } } }