那个html中验证码能用java去写吗,如果可以我想要一份详细的代码 .

 我来答
匿名用户
2013-08-10
展开全部
完全可以,本来就是JAVA的活。给你个DEMO: 第一步:编写一个产生验证码的Servlet (验证码为全数字)package org.yeeku.web;import java.awt.Color;import java.awt.Font;import java.awt.Graphics;import java.awt.image.BufferedImage;import java.io.IOException;import java.util.Random;import javax.imageio.ImageIO;import javax.servlet.ServletException;import javax.servlet.ServletOutputStream;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;public class AuthImg extends HttpServlet {protected void processRequest(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {response.setContentType("image/jpeg");response.setHeader("Pragma","No-cache");response.setHeader("Cache-Control","no-cache");response.setDateHeader("Expires", 0); HttpSession session=request.getSession();// 在内存中创建图象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<6;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();ServletOutputStream responseOutputStream =response.getOutputStream();// 输出图象到页面ImageIO.write(image, "JPEG", responseOutputStream);//以下关闭输入流!responseOutputStream.flush();responseOutputStream.close();}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);}// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">protected void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {processRequest(request, response);}protected void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {processRequest(request, response);}public String getServletInfo() {return "Short description";}// </editor-fold>}//产生的验证码有字母的Servletpublic class AuthImg extends HttpServlet{private Font mFont = new Font("Arial Black", Font.PLAIN, 16);public void init() throws ServletException{super.init();}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);}public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{response.setHeader("Pragma","No-cache");response.setHeader("Cache-Control","no-cache");response.setDateHeader("Expires", 0);response.setContentType("image/jpeg");int width=100, height=18;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(1, 1, width-1, height-1);g.setColor(new Color(102,102,102));g.drawRect(0, 0, width-1, height-1);g.setFont(mFont);g.setColor(getRandColor(160,200));for (int i=0;i<155;i++){int x = random.nextInt(width - 1);int y = random.nextInt(height - 1);int xl = random.nextInt(6) + 1;int yl = random.nextInt(12) + 1;g.drawLine(x,y,x + xl,y + yl);}for (int i = 0;i < 70;i++){int x = random.nextInt(width - 1);int y = random.nextInt(height - 1);int xl = random.nextInt(12) + 1;int yl = random.nextInt(6) + 1;g.drawLine(x,y,x - xl,y - yl);}String sRand="";for (int i=0;i<6;i++){String tmp = getRandomChar();sRand += tmp;g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));g.drawString(tmp,15*i+10,15);}HttpSession session = request.getSession(true);session.setAttribute("rand",sRand);g.dispose();ImageIO.write(image, "JPEG", response.getOutputStream());}private String getRandomChar(){int rand = (int)Math.round(Math.random() * 2);long itmp = 0;char ctmp = '\';switch (rand){case 1:itmp = Math.round(Math.random() * 25 + 65);ctmp = (char)itmp;return String.valueOf(ctmp);case 2:itmp = Math.round(Math.random() * 25 + 97);ctmp = (char)itmp;return String.valueOf(ctmp);default :itmp = Math.round(Math.random() * 9);return String.valueOf(itmp);}}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
匿名用户
2013-08-10
展开全部
看了上面的代码...基本都说到了
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式