JSP彩色验证码的问题. 你在getOutputStream的时候出问题了,你在你的JSP页面中绝不能有HTML的元素,也不要有多余的回车<%%><%//必须这个紧挨着%> 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 TO: ChDw(米) 整个生成图片的image.jsp是这样的:<%@ 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);}// 将认证码存入SESSIONsession.setAttribute("rand",sRand);// 图象生效g.dispose();// 输出图象到页面ImageIO.write(image, "JPEG", response.getOutputStream());%>请帮我指出问题的所在.谢谢 我写的生成图片保存到硬盘上的代码File desFile = new File(file);java.io.FileOutputStream fio = new java.io.FileOutputStream(desFile);JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(fio);encoder.encode(image);fio.close();把输出流改一下就可以了 to: CarpAndCat(Rock) 能指明,怎么样做吗? 关于字符串的equals与==的问题 如何判断数组元素包含在另外的数组里 帮忙看下这段代码~~ 请教,为什么编译有错误的问题,还可以运行呢? 关于NoClassDefFoundError错误的排错 这个错误如何解决呢 十万火急的求助一个小项目!! 这里的FAQ好乱,只好发新帖提问! 关于Dialog的问题请指教!谢谢. 随机在文件中读取一个汉字 小妹请问thinking in java 和Core Java 2这两本名书对有一点java基础的初学者来书哪本先看为好?? 对Applet 有没有好一点的,快速一点的掌握方法!不是急于求成,想请教一点经验!比较急切!在线等!
<%@ 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());
%>
请帮我指出问题的所在.谢谢
File desFile = new File(file);
java.io.FileOutputStream fio = new java.io.FileOutputStream(desFile);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(fio);
encoder.encode(image);
fio.close();
把输出流改一下就可以了