81,092
社区成员
发帖
与我相关
我的任务
分享
有可能是验证码的问题
<%@ page contentType="image/jpeg" import="java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*,java.text.*,com.cloudsoar3c.tools.*" pageEncoding="utf-8"%>
<%@page import="java.io.OutputStream"%>
<%@page import="java.awt.geom.Arc2D"%>
<%!String[] fonts = { "宋体", "楷体","黑体","Arial","Times New Roman"};// 字体
//String[] fonts2 = {"华文彩云"};// 字体
//int[] styles = { Font.PLAIN, Font.BOLD, Font.ITALIC,
// Font.BOLD + Font.ITALIC };// 样式
int[] styles2 = { Font.PLAIN, Font.ITALIC, Font.ITALIC, Font.ITALIC };// 样式
//int[] sizes = { 14, 15, 16, 17, 18, 19, 20 };// 字体大小
//int[] sizes2 = { 18, 19, 20, 18, 19, 20, 20 };// 字体大小
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);
}
%>
<%
try {
//设置页面不缓存
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
// 在内存中创建图象
int width = 3000, height = 500;
BufferedImage image = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
BufferedImage image_error = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
// 获取图形上下文
Graphics gr = image.getGraphics();
Graphics2D g = (Graphics2D) gr;
g.setStroke(new BasicStroke(3));//设置线条的粗细
//生成随机类
Random random = new Random();
// 设定背景色
g.setColor(getRandColor(200, 250));
g.fillRect(0, 0, width, height);
g.drawRect(0, 0, width - 1, height - 1);
String sRand = Code.getCode(session);//rand
g.setColor(getRandColor(10, 100));
g.setFont(new Font(fonts[0], styles2[0], 550));
g.drawString(sRand, 5, 450);
// 图象生效
g.dispose();
ImageIO.write(image, "JPEG", response.getOutputStream());
out.clear();
out=pageContext.pushBody();
} catch (Exception e) {
e.printStackTrace();
}
%>
public static String getCode(HttpSession session) {
boolean hasBrackets = false;
float num1, num2, num3, result = -1;
char op1, op2;
text = new String();
boolean running = true;
try {
do {
num1 = getNum(1, 5);
num2 = getNum(1, 5);
num3 = getNum(1, 5);
op1 = getOp();
op2 = getOp();
text = new String();
hasBrackets = false;
if (op1 == op2) {
continue;
}
hasBrackets = hasBrackets();
// 添加括号
if (hasBrackets) {
result = getBracketsStr(num1, num2, num3, op1, op2, position);
if (result > 0) {
running = false;
}
} else {
result = getStr(num1, num2, num3, op1, op2);
if (result > 0) {
running = false;
}
}
} while (running);
byte[] encryptResult = Encrypt.encrypt((((int) result) + "").toLowerCase());
String encryptResultStr = Encrypt.parseByte2HexStr(encryptResult);
Util.log("Code.java code = " + ((int) result) + "||encryptResultStr = " + encryptResultStr);
session.setAttribute("rand", encryptResultStr);
} catch (Exception e) {
e.printStackTrace();
}
return text;
}