在网上找了了一个JSP 随机产生验证码的程序(如下),在其他的机器上能工作正常验证码也可正常显示,可是在我机器上输出的图片只有一个XX,是配置上的问题么?
我服务器的配置resin3.X+jdk1.4+apache2.0<%@ page contentType="image/jpeg" import="java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*" %>
<%!
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);
}
%>
<%
//设置页面不缓存
response.setHeader("Pragma","No-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires", 0);// 在内存中创建图象
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("rand",sRand);
// 图象生效
g.dispose();// 输出图象到页面
ImageIO.write(image, "JPEG", response.getOutputStream());
%>
我服务器的配置resin3.X+jdk1.4+apache2.0<%@ page contentType="image/jpeg" import="java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*" %>
<%!
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);
}
%>
<%
//设置页面不缓存
response.setHeader("Pragma","No-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires", 0);// 在内存中创建图象
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("rand",sRand);
// 图象生效
g.dispose();// 输出图象到页面
ImageIO.write(image, "JPEG", response.getOutputStream());
%>
就是在图片输出的时候是个XX .
而我把同样的程序给另一个朋友运行他那边是可以正常输出随机码的.
我有点怀疑是环境问题,可又不知道是哪里出问题了.
上面,
你整合的可能有些问题。还有,就是你有没有采用什么框架??
我指你的程序?
我在做apache+tomcat的时候就碰到过。后来还是解决了。在修改了服务器配置。
resin我不懂。不好意思,不能帮你拉!
多谢各位朋友热心的解答.suhm89 你也是用resin 做web服务器么??把resin换成tomcat是不太可能的,不知有没有其他生成随机码的程序可否帖上来参考?
import javax.servlet.http.*;
import java.io.*;
import javax.servlet.http.HttpSession;
import java.awt.Color;
import java.util.Random;
import java.awt.image.BufferedImage;
import java.awt.Graphics;
import java.awt.Font;
import javax.imageio.ImageIO;public class ValidCodeServlet
extends HttpServlet {
public ValidCodeServlet() {
} 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);
} protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws
ServletException, IOException { //设置页面不缓存
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);// 在内存中创建图象
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( Color.WHITE );
g.fillRect(0, 0, width, height);//设定字体
g.setFont(new Font("Times New Roman", Font.BOLD, 16));//画边框
//g.setColor(new Color());
//g.drawRect(0,0,width-1,height-1);
// 随机产生155条干扰线,使图象中的认证码不易被其它程序探测到
g.setColor(getRandColor(200, 225));
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);
}
HttpSession session = request.getSession( true );
session.setAttribute( "validcode", sRand ); g.dispose(); ImageIO.write(image, "JPEG", response.getOutputStream());
}
}
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.rmi.ServerException;
import java.util.Random;import javax.imageio.ImageIO;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;public class ImageServlet extends HttpServlet
{
private final int width = 60;
private final int height = 25; public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServerException, IOException
{
try
{
// 设置httpresponse的输出格式
response.setContentType("image/jpeg");
// 设置页面不缓存
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
BufferedImage image = getBufferedImage();
Graphics graphics = getGraphics(image);
drawLine(graphics, 150);
drawCheckWord(graphics);
// 输出图象到页面
ImageIO.write(image, "JPEG", response.getOutputStream()); }
catch (Exception ex)
{
ex.printStackTrace();
}
} public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServerException, IOException
{
try
{
doGet(request, response);
}
catch (Exception ex)
{
ex.printStackTrace();
}
} // 给定范围获得随机颜色
public 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);
} // 随机产生lineCount条干扰线,使图象中的认证码不易被其它程序探测到
public void drawLine(Graphics g, int lineCount)
{
g.setColor(getRandColor(160, 200));
Random random = new Random();
for (int i = 0; i < lineCount; 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);
}
} public BufferedImage getBufferedImage()
{
// 在内存中创建图象
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); return image;
} public Graphics getGraphics(BufferedImage image)
{
// 获取图形上下文
Graphics g = image.getGraphics();
g.setColor(getRandColor(200, 250));
g.fillRect(0, 0, width, height);
g.setFont(new Font("Times New Roman", Font.PLAIN, 20));
return g;
} public void drawCheckWord(Graphics g)
{
String sRand = "";
Random random = new Random();
for (int i = 0; i < 4; i++)
{
String rand = getCheckWord(random.nextInt(63));
sRand += rand;
// 将认证码显示到图象中
// 调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成
g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110)));
g.drawString(rand, 13 * i + 6, 18);
}
System.out.println("===========+" + sRand);
} // 获取随机码
public String getCheckWord(int position)
{
if (position > 62)
position = 40;
int hashCodeList[] = new int[63];
hashCodeList[0] = 35;
for (int i = 1; i <= 10; i++)
{
hashCodeList[i] = 47 + i;
}
for (int i = 11; i < 37; i++)
{
hashCodeList[i] = 54 + i;
}
for (int i = 37; i < 63; i++)
{
hashCodeList[i] = 60 + i;
}
return String.valueOf((char) hashCodeList[position]);
}
}