---
title: "用jsp做个有彩纹的随机码，----可直接生成"
author: "fuyangchang"
date: "2007-06-26T13:22:00Z"
tags: []
slug: "jsp"
draft: false
lastmod: "2007-06-26T13:22:00Z"
id: 104067
url: "https://csdn.iteye.com/blog/104067"
---

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;%@ page contentType="image/jpeg" import="java.awt.\*,java.awt.image.\*,java.util.\*,javax.imageio.\*" %&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;%!  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)Color getRandColor(int fc,int bc)...{//给定范围获得随机颜色  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Random random = new Random();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)if(fc&gt;255) fc=255;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)if(bc&gt;255) bc=255;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)int r=fc+random.nextInt(bc-fc);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)int g=fc+random.nextInt(bc-fc);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)int b=fc+random.nextInt(bc-fc);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)return new Color(r,g,b);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)%&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;%  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)//设置页面不缓存  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)response.setHeader("Pragma","No-cache";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)response.setHeader("Cache-Control","no-cache";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)response.setDateHeader("Expires", 0);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)// 在内存中创建图象  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)int width=60, height=20;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE\_INT\_RGB);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)// 获取图形上下文  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)Graphics g = image.getGraphics();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)//生成随机类  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)Random random = new Random();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)// 设定背景色  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)g.setColor(getRandColor(200,250));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)g.fillRect(0, 0, width, height);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)//设定字体  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)g.setFont(new Font("Times New Roman",Font.PLAIN,18));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)//画边框  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)g.setColor(new Color(255,255,255));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)g.drawRect(0,0,width-1,height-1);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)// 随机产生155条干扰线，使图象中的认证码不易被其它程序探测到  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)g.setColor(getRandColor(160,200));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)for (int i=0;i&lt;155;i++)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)int x = random.nextInt(width);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)int y = random.nextInt(height);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)int xl = random.nextInt(12);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)int yl = random.nextInt(12);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)g.drawLine(x,y,x+xl,y+yl);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)// 取随机产生的认证码(4位数字)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)String sRand="";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)for (int i=0;i&lt;4;i++)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)String rand=String.valueOf(random.nextInt(10));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)sRand+=rand;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)// 将认证码显示到图象中  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//调用函数出来的颜色相同，可能是因为种子太接近，所以只能直接生成  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)g.drawString(rand,13\*i+6,16);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)// 将认证码存入SESSION  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)session.setAttribute("rand",sRand);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)// 图象生效  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)g.dispose();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)// 输出图象到页面  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)ImageIO.write(image, "JPEG", response.getOutputStream());  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)%&gt;