« | August 2025 | » | 日 | 一 | 二 | 三 | 四 | 五 | 六 | | | | | | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | | | | | | | |
| 公告 |
戒除浮躁,读好书,交益友 |
Blog信息 |
blog名称:邢红瑞的blog 日志总数:523 评论数量:1142 留言数量:0 访问次数:9697830 建立时间:2004年12月20日 |

| |
[java语言]网站验证码的若干问题 原创空间, 文章收藏, 软件技术
邢红瑞 发表于 2005/7/21 14:22:24 |
显示附加码的主要问题是能够在不支持XWindows的linux环境下显示图片,
毕竟大型的网站是基于linux操作系统,同时一般不会安装XWindow。Linux下的JDK1.4中的awt功能,
要用到 XWindow 的一些库,所以需要安装。
如果你使用的是JDK1.5 + Tomcat,就不会有这个问题,JDK1.5重写awt库了。
如果是在linux或者unix下面使用验证码,用xwindows启动系统,启动tomcat,
可以解决在linux下面不能显示验证码的问题。
可以使用export JAVA_OPTS="-Djava.awt.headless=true",
在catalina.sh中增加CATALINA_OPTS="$CATALINA_OPTS -Djava.awt.headless=true"
在启动Weblogic中要加上这个参数-Djava.awt.headless=true,这样才能显示出图片。
在JDK1.4可以在代码中增加:
System.setProperty("java.awt.headless", "true");
这样就不用xWindow库的支持了。
如果出现tomcat的log中有异常:
StandardWrapperValve[jsp]: Servlet.service() for servlet jsp threw exception
org.apache.jasper.JasperException: getOutputStream() has already been called for this response
这是因为apache的jasper2已经在翻译jsp为servlet时,已经调用了了this response的getWriter,
你再调用getOutputStream()时,就抛出这个异常。注意,weblogic和resin的HttpResponse实现没有遵循sun的
标准,所以可以直接输出图片。如果显示不正常,加上out.clear();在weblogic中显示正常了。
resin翻译的jsp可能产生几个空行,影响显示。
附两个个例子
import java.io.*;
import javax.servlet.http.*;
import javax.servlet.*;
import java.util.*;
import java.awt.*;
import java.awt.image.*;
import javax.imageio.*;
//import com.sun.image.codec.jpeg.*;
public class vImage extends HttpServlet
{
public void init(ServletConfig conf) throws ServletException
{
super.init(conf);
}
public void
doGet(HttpServletRequest req,HttpServletResponse res) throws
ServletException, IOException
{
res.setContentType("image/jpeg");
res.setHeader("Pragma","No-cache");
res.setHeader("Cache-Control","no-cache");
res.setDateHeader("Expires", 0);
HttpSession session = req.getSession();
// 在内存中创建图象
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(new Font("Times New Roman",Font.PLAIN,18));
// 画边框
//g.setColor(new Color());
//g.drawRect(0,0,width-1,height-1);
// 随机产生155条干扰线,使图象中的认证码不易被其它程序探测到
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);
}
// 取随机产生的认证码(4位数字)
String sRand="";
for (int i=0;i<4;i++)
{
String rand=String.valueOf(random.nextInt(10));
sRand+=rand;
// 将认证码显示到图象中
g.setColor(new
Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));
// 调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成
g.drawString(rand,13*i+6,16);
}
// 将认证码存入SESSION
session.setAttribute("post_validate_code",sRand);
// 图象生效
g.dispose();
// 输出图象到页面
ImageIO.write(image, "JPEG", res.getOutputStream());
//JPEGImageEncoder encoder =
JPEGCodec.createJPEGEncoder(res.getOutputStream());
//encoder.encode(image);
}
public void
doPost(HttpServletRequest req,HttpServletResponse res) throws
ServletException, IOException
{
doGet(req,res);
}
//给定范围获得随机颜色
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);
}
}
import java.io.*;
import java.util.*;
import com.sun.image.codec.jpeg.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.awt.*;
import java.awt.image.*;
/**
* Title: getImg.java
* Description: 这个class主要实现随机生成一个4位数的验证码,并写入session,
* Copyright: Copyright (c) 2003
* Company: 蓝星软件
* @author falcon
* @version 1.1
*/
public class getImg extends HttpServlet {
private Font mFont=new Font("宋体", Font.PLAIN,12);//设置字体
//处理post
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException {
doGet(request,response);
}
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException {
//取得一个1000-9999的随机数
String s="";
int intCount=0;
intCount=(new Random()).nextInt(9999);//
if(intCount<1000)intCount+=1000;
s=intCount+"";
//对session付值。
HttpSession session=request.getSession (true);
session.setAttribute("getImg",s);
response.setContentType("image/gif");
ServletOutputStream out=response.getOutputStream();
BufferedImage image=new BufferedImage(35,14,BufferedImage.TYPE_INT_RGB);
Graphics gra=image.getGraphics();
//设置背景色
gra.setColor(Color.yellow);
gra.fillRect(1,1,33,12);
//设置字体色
gra.setColor(Color.black);
gra.setFont(mFont);
//输出数字
char c;
for(int i=0;i<4;i++) {
c=s.charAt(i);
gra.drawString(c+"",i*7+4,11); //7为宽度,11为上下高度位置
}
JPEGImageEncoder encoder=JPEGCodec.createJPEGEncoder(out);
encoder.encode(image);
out.close();
}
} |
|
|