1 import Java.awt.image.buffered image; 2 importjava.io.BufferedReader; 3
4 importjava.awt.Color; 5 importjava.io.FileReader; 6 importjava.io.IOException; 7 importjava.util.ArrayList; 8 importjava.util.List; 9 importjava.util.Random; 10
11 importjava.awt.Font; 12 importjava.awt.Graphics; 13 importjava.awt.Graphics2D; 14
15 importjavax.imageio.ImageIO; 16 import javax.servlet.servlet exception; 17 import javax.servlet.http.http servlet; 18 import javax.servlet.http.http servlet request; 19 import javax.servlet.http.http servlet response; 20
21 /**
22 *验证码生成程序23 */
24 publicclasscheckchengyuextendshttp servlet {2}
26 //在集合中保存所有成语
27 privatelistwords=new ArrayList (; 28
29 @ override 30 public void init (throwsservletexception ) 31//要在初始化阶段从qy95.txt32 //web项目读取文件,请使用绝对磁盘路径
33 String path=getServletContext ().getrealpath )/weB-INF/chengyu.txt; 34 try { 35 bufferedreaderreader=newbufferedreader (new filereader ) path ); 36字符串行; 37while((line=reader.readline ) )!=null6538Words.add(line ); 39 ) 40reader.close ); 41 ) catch(ioexceptione ) {42 e.printStackTrace ); 43(44 ) 45
46公共语音输出(httpservletrequestrequest,HttpServletResponse response ) throwsServletException,ioexception ) 47/高速缓存50//response.set date header (' expires ',-1);
51
52 int width=120; 53 int height=30; 54
55 //步骤1描绘存储器内的图像
56 bufferedimagebufferedimage=newbufferedimage (width,height,BufferedImage.TYPE_INT_RGB ); 57
58 //步骤2用图像描绘背景色---通过绘图对象
59 graphics graphics=buffered image.get graphics (; //描绘对象---刷子60 //在描绘什么图形之前必须指定颜色
61 graphics.setcolor (get randcolor ) 200,250 ); 62 graphics.fillrect (0,0,width,height ); 63
64 //步骤3绘制边框
65 graphics.setcolor (color.white; 66 graphics.drawrect (0,0,width - 1,height - 1 ); 67
68 //步骤4、4的随机数字
69 graphics 2d graphics 2d=(graphics 2d ) graphics; 70 //设定输出字体
71graphics2d.setfont(newfont ('宋体',Font.BOLD,18 ); 72
73 Random random=new Random (; //生成随机数
74 intindex=random.nextint (words.size ) ); 75stringword=words.get(index ); //获得习语76
77 //定义坐标
78 int x=10; 79for(intI=0; i word.length (; I () {80 //随机颜色
81graphics2d.setcolor(newcolor ) 20random.nextint(110 )、20random.nextint(110 )、20random.nextint ) ) )
8283 //转-30 --- 30度
4intJiaodu=random.nextint(60 )- 30; 85 //弧度换算
86 double theta=jiao du * math.pi/180; 87
88 //获取字母数字字符
89charc=word.Charat(I; 90
将91 //输出到图像
92graphics2d.rotate(Theta,x,20 ); 93 graphics 2d.drawstring (string.value of (c ),x,20 ); 94graphics2d.rotate(-Theta,x,20 ); 95 x =30; (97 ) 97
98 //将认证码的内容保存到session
99 request.getSession ().set attribute (' check code _ session ',word ); 100
101 //步骤5画出干涉线
102 graphics.setcolor (get randcolor (160,200 ); 103英寸1; 104合tx2; 105 inty1; 106 inty2; 107for(intI=0; i 30; I ) 108x1=random.nextint(width ); 109x2=random.nextint(12 ); 10y1=random.nextint(height ); 11y2=random.nextint(12; 12graphics.Drawline(x1,y1,x1 x2,x2 y2 ); 113 ) 114
115 //以上的图像输出到浏览器ImageIO
116 graphics.dispose (; //释放资源117
118 //将图像写入response.getOutputStream ()
19imageio.write(bufferedimage,' jpg ',response.getOutputStream () ); 120
121 ) 122
123公共void dopost (httpservletrequestrequest,HttpServletResponse response ) throwsServletException,ioexception ) 124
127 /**
128 *某范围的color129 *130 *@paramfc int范围参数1131 *@parambc int范围参数2132 *@returnColor133 */
134专用颜色转换颜色(intbc,intbc ) {135 //取其随机颜色
136 Random random=newRandom (; 137if(fc255 ) {138 fc=255; 139 ) 140if(BC255 ) {141 bc=255; 142 ) 143intr=fcrandom.nextint(BC-fc ); 144intg=fcrandom.nextint(BC-fc ); 145intB=fcrandom.nextint(BC-fc ); 146返回新颜色(r,g,b ); 147 ) 148
149 }