Java生成验证码源代码 java怎么实现验证码的功能( 四 )


//request.setCharacterEncoding("UTF-8");
int width=100;
int height=50;
//获得一张图片
BufferedImage image=new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g=image.getGraphics();
g.setColor(Color.WHITE);
g.fillRect(1, 1, width-2, height-2);
g.setFont(new Font("宋体",Font.BOLD,30));
Random random=new Random();
//填充的字符串
String str="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
//缓存生成的验证码
StringBuffer stringbuffer=new StringBuffer();
//随机生成验证码的颜色和字符
for(int i=0;i4;i++)
{//设置随机颜色
g.setColor(new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)));
int index=random.nextInt(62);//这里的62就是从填充字符段中随意选取一个位置
String str1=str.substring(index,index+1);
g.drawString(str1, 20*i, 30);//x,y数值设置太小会显示不出来
stringbuffer.append(str1);
}
//将生成的验证码存到服务器
request.getSession().setAttribute("checkcode", stringbuffer.toString());//key和value
//将图片发送给浏览器
ImageIO.write(image, "jpg", response.getOutputStream());
}
}
用户登录界面代码
package s1;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class Login extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");// 设置服务器发送给浏览器的编码方式
request.setCharacterEncoding("UTF-8"); // 客户端向服务器提交的数据的解码方式
// 获得用户提交的数据
String checkcode = request.getParameter("checkcode");
System.out.println(checkcode);
// 判断输入的验证码是不是符合
HttpSession session = request.getSession();// session是存放数据的地方
String str = (String) session.getAttribute("checkcode");
if (str != null) {
if (checkcode.compareToIgnoreCase(str) == 0) // 验证码忽略大小写
response.getWriter().println("验证码输入正确");
else
response.getWriter().println("验证码输入错误");
}
else response.getWriter().println("验证码失效");
// 使用完的验证码信息要删除,返回原页面再输一次,验证码就失效了
session.removeAttribute("checkcode");
}
}
怎么用Java代码实现一个验证码,求具体实现方法package util;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;
import javax.imageio.ImageIO;
public final class ImageUtil {
// 验证码字符集
private static final char[] chars = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
// 字符数量
private static final int SIZE = 4;
// 干扰线数量
private static final int LINES = 5;
// 宽度
private static final int WIDTH = 80;
// 高度

推荐阅读