Jsp图片验证码及刷新Word文档下载推荐.docx
- 文档编号:3958138
- 上传时间:2023-05-02
- 格式:DOCX
- 页数:19
- 大小:17.03KB
Jsp图片验证码及刷新Word文档下载推荐.docx
《Jsp图片验证码及刷新Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《Jsp图片验证码及刷新Word文档下载推荐.docx(19页珍藏版)》请在冰点文库上搜索。
*/
publicclassHelloImage{
publicstaticvoidmain(String[]args){
BufferedImageimage=newBufferedImage(80,50,
BufferedImage.TYPE_INT_RGB);
//建立BufferedImage对象。
指定图片的长度宽度和色彩。
Graphicsg=image.getGraphics();
//取得Graphics对象,用来绘制图片。
g.setColor(newColor(255,255,255));
//绘制图片背景和文字。
g.fillRect(0,0,80,25);
g.setColor(newColor(0,0,0));
g.drawString("
HelloImage"
6,16);
g.dispose();
//释放Graphics对象所占用的资源。
try{
ImageIO.write(image,"
jpeg"
newFile("
C:
\\helloImage.jpeg"
));
//通过ImageIO对象的write静态方法将图片输出。
}catch(IOExceptione){
e.printStackTrace();
}
}
/**
*生成图片
*/
publicclassHelloImage{
publicstaticvoidmain(String[]args){
BufferedImageimage=newBufferedImage(80,50,
try{
}catch(IOExceptione){
}
}
********************************************************
********************************************************viewplaincopytoclipboardprint?
2.生成验证码图片的类
2.生成验证码图片的类viewplaincopytoclipboardprint?
importjava.awt.Font;
importjava.util.Random;
importjavax.servlet.http.HttpServletResponse;
*生成验证码图片的类
publicclassRandImgCreater{
privatestaticfinalStringCODE_LIST="
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890"
;
privateHttpServletResponseresponse=null;
privatestaticfinalintHEIGHT=20;
privatestaticfinalintFONT_NUM=4;
privateintwidth=0;
privateintiNum=0;
privateStringcodeList="
privatebooleandrawBgFlag=false;
privateintrBg=0;
privateintgBg=0;
privateintbBg=0;
publicRandImgCreater(HttpServletResponseresponse){
this.response=response;
this.width=13*FONT_NUM+12;
this.iNum=FONT_NUM;
this.codeList=CODE_LIST;
publicRandImgCreater(HttpServletResponseresponse,intiNum,StringcodeList){
this.width=13*iNum+12;
this.iNum=iNum;
this.codeList=codeList;
publicStringcreateRandImage(){
BufferedImageimage=newBufferedImage(width,HEIGHT,
Randomrandom=newRandom();
if(drawBgFlag){
g.setColor(newColor(rBg,gBg,bBg));
g.fillRect(0,0,width,HEIGHT);
}else{
g.setColor(getRandColor(200,250));
for(inti=0;
i<
155;
i++){
g.setColor(getRandColor(140,200));
intx=random.nextInt(width);
inty=random.nextInt(HEIGHT);
intxl=random.nextInt(12);
intyl=random.nextInt(12);
g.drawLine(x,y,x+xl,y+yl);
g.setFont(newFont("
TimesNewRoman"
Font.PLAIN,18));
StringsRand="
for(inti=0;
i<
iNum;
i++){
intrand=random.nextInt(codeList.length());
StringstrRand=codeList.substring(rand,rand+1);
sRand+=strRand;
g.setColor(newColor(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));
g.drawString(strRand,13*i+6,16);
JPEG"
response.getOutputStream());
returnsRand;
publicvoidsetBgColor(intr,intg,intb){
drawBgFlag=true;
this.rBg=r;
this.gBg=g;
this.bBg=b;
privateColorgetRandColor(intfc,intbc){
if(fc>
255)
fc=255;
if(bc>
bc=255;
intr=fc+random.nextInt(bc-fc);
intg=fc+random.nextInt(bc-fc);
intb=fc+random.nextInt(bc-fc);
returnnewColor(r,g,b);
*生成验证码图片的类
publicclassRandImgCreater{
publicRandImgCreater(HttpServletResponseresponse){
publicRandImgCreater(HttpServletResponseresponse,intiNum,StringcodeList){
publicStringcreateRandImage(){
BufferedImageimage=newBufferedImage(width,HEIGHT,
if(drawBgFlag){
}else{
i++){
i++){
publicvoidsetBgColor(intr,intg,intb){
privateColorgetRandColor(intfc,intbc){
255)
******************************************************
******************************************************viewplaincopytoclipboardprint?
3.调用生成验证码图片:
img.jsp"
<
PREclass=javaname="
code"
>
%@pagecontentType="
image/jpeg"
import="
liuyanban.*"
%>
%
response.setHeader("
Pragma"
"
No-cache"
);
Cache-Control"
no-cache"
response.setDateHeader("
Expires"
0);
RandImgCreaterrc=newRandImgCreater(response);
Stringrand=rc.createRandImage();
session.setAttribute("
rand"
rand);
%>
/PRE>
****************************************************<
<
4.测试,验证,刷新:
%response.setHeader("
response.setHeader("
response.setDateHeader("
RandImgCreaterrc=newRandImgCreater(response);
Stringrand=rc.createRandImage();
session.setAttribute("
viewplaincopytoclipboardprint?
%
********************************************************************************************************
4.测试
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Jsp 图片 验证 刷新