馬光軍--------BLOG

          BlogJava 聯(lián)系 聚合 管理
            1 Posts :: 25 Stories :: 5 Comments :: 0 Trackbacks
          MakePicture.java產(chǎn)生圖片的類:
          package com.strongit.zwjc.util;

          import java.io.*;
          import javax.servlet.*;
          import javax.servlet.http.*;
          import java.awt.*;
          import java.awt.image.*;
          import java.util.*;
          import javax.imageio.*;


          public class MakePicture 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();
                  
          // 在內(nèi)存中創(chuàng)建圖象
                  int width=60, height=20;
                  BufferedImage image 
          = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

                  
          // 獲取圖形上下文
                  Graphics g = image.getGraphics();

                  
          //生成隨機(jī)類
                  Random random = new Random();

                  
          // 設(shè)定背景色
                  g.setColor(getRandColor(200,250));
                  g.fillRect(
          00, width, height);

                  
          //設(shè)定字體
                  g.setFont(new Font("Times New Roman",Font.PLAIN,18));

                  
          //畫(huà)邊框
                  
          //g.setColor(new Color());
                  
          //g.drawRect(0,0,width-1,height-1);


                  
          // 隨機(jī)產(chǎn)生155條干擾線,使圖象中的認(rèn)證碼不易被其它程序探測(cè)到
                  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);
                  }

                  
          // 取隨機(jī)產(chǎn)生的認(rèn)證碼(4位數(shù)字)
                  String sRand="";
                  
          for (int i=0;i<4;i++){
                      String rand
          =String.valueOf(random.nextInt(10));
                      sRand
          +=rand;
                      
          // 將認(rèn)證碼顯示到圖象中
                      g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));//調(diào)用函數(shù)出來(lái)的顏色相同,可能是因?yàn)榉N子太接近,所以只能直接生成
                      g.drawString(rand,13*i+6,16);
                  }

                  
          // 將認(rèn)證碼存入SESSION
                  session.setAttribute("rand",sRand);
                  
          // 圖象生效
                  g.dispose();
                  ServletOutputStream responseOutputStream 
          =response.getOutputStream();
                  
          // 輸出圖象到頁(yè)面
                  ImageIO.write(image, "JPEG", responseOutputStream);

                  
          //以下關(guān)閉輸入流!
                  responseOutputStream.flush();
                  responseOutputStream.close();
              }
              Color getRandColor(
          int fc,int bc){//給定范圍獲得隨機(jī)顏色
                  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);
              }
             
              
          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";
              }

          }
          頁(yè)面代碼:
          <%@ page language="java" pageEncoding="UTF-8"%>
          <html>
              
          <head>
                  
          <meta http-equiv="pragma" content="no-cache">
                  
          <meta http-equiv="cache-control" content="no-cache">
                  
          <meta http-equiv="expires" content="0">
                  
          <link href="css/datagather_login.css" rel="stylesheet" type="text/css">
                  
          <title>個(gè)性化門戶登陸頁(yè)面</title>
                  
          <script type="text/javascript">
                      
          var request = false;
                      
          function refresh(){
                            document.getElementById(
          "makePicture").src="makePicture?now="+ new Date();
                        }
                  
          </script>
              
          </head>
              
          <body>
                  
          <form action="" method="post" id="form1" name="form1">
                      
          <table width="100%" height="100%" border="0" cellspacing="0"
                          cellpadding
          ="0">
                          
          <tr>
                              
          <td align="center">
                                  
          <table width="607" align="center" border="0" cellspacing="0"
                                      cellpadding
          ="0">
                                      
          <tr>
                                          
          <td valign="bottom">
                                              
          <img src="images/portlettools/login/titleportal.jpg"
                                                  width
          ="682" height="103" />
                                          
          </td>
                                      
          </tr>
                                      
          <tr>
                                          
          <td valign="top" align="right"
                                              background
          ="images/portlettools/login/bg.jpg" width="607"
                                              height
          ="288">
                                              
          <table width="38%" border="0" cellspacing="0" cellpadding="0">
                                                  
          <tr>
                                                      
          <td height="30">
                                                          
          &nbsp;
                                                      
          </td>
                                                  
          </tr>
                                                  
          <tr>
                                                      
          <td height="100">
                                                          
          <table width="100%" height="100" border="0" cellspacing="0"
                                                              cellpadding
          ="0">
                                                              
          <tr>
                                                                  
          <td width="52" class="tt">
                                                                      用戶名:
                                                                  
          </td>
                                                                  
          <td colspan="2" class="lo_input">
                                                                      
          <input name="username" height="40" type="text"
                                                                          style
          ="width:132" onfocus="true" class="lo_input"
                                                                          size
          ="18" />
                                                                  
          </td>
                                                              
          </tr>
                                                              
          <tr>
                                                                  
          <td width="52" class="tt">
                                                                      密 碼:
                                                                  
          </td>
                                                                  
          <td colspan="2" class="lo_input">
                                                                      
          <input id="password" name="password" type="password"
                                                                          style
          ="width:132"
                                                                          onkeydown
          ="if(event.keyCode==13) gotoSubmit();"
                                                                          size
          ="18" />
                                                                  
          </td>
                                                              
          </tr>
                                                              
          <tr>
                                                                  
          <td width="52" class="tt">
                                                                      驗(yàn)證碼:
                                                                  
          </td>
                                                                  
          <td width="70">
                                                                      
          <input type="text" name="checkCode" size="4"
                                                                          maxlength
          ="4"
                                                                          onkeydown
          ="if(event.keyCode==13) gotoSubmit();">
                                                                  
          </td>
                                                                  
          <td>
                                                                      
          <img src="makePicture" id="makePicture">
                                                                  
          </td>
                                                              
          </tr>
                                                              
          <tr>
                                                                  
          <td width="52"></td>
                                                                  
          <td>
                                                                  
          <td>
                                                                      
          <font color="blue"><href="#" onclick="refresh()">看不清</a>
                                                                      
          </font>
                                                                  
          </td>
                                                              
          </tr>
                                                          
          </table>
                                                      
          </td>
                                                  
          </tr>
                                                  
          <tr>
                                                      
          <td>
                                                          
          <table width="210" align="left" border="0" cellspacing="0"
                                                              cellpadding
          ="0" height="80">
                                                              
          <tr>
                                                                  
          <td align="left">
                                                                      
          <img src="images/portlettools/login/login.jpg" width="76"
                                                                          height
          ="35" onclick="gotoSubmit();" style="cursor:hand;" />
                                                                  
          </td>
                                                                  
          <td>
                                                                      
          <img src="images/portlettools/login/cancle.jpg"
                                                                          width
          ="76" height="35" style="cursor:hand;"
                                                                          onclick
          ="gotoReset()" />
                                                                  
          </td>
                                                              
          </tr>
                                                          
          </table>
                                                      
          </td>
                                                  
          </tr>
                                              
          </table>
                                          
          </td>
                                      
          </tr>
                                  
          </table>
                              
          </td>
                          
          </tr>
                      
          </table>
                  
          </form>
              
          </body>
          </html>
          WEB.XML中設(shè)置:
          <servlet>
                  
          <servlet-name>makePicture</servlet-name>
                  
          <servlet-class>
                      com.strongit.zwjc.util.MakePicture
                  
          </servlet-class>
              
          </servlet>
              
          <servlet-mapping>
                  
          <servlet-name>makePicture</servlet-name>
                  
          <url-pattern>/makePicture</url-pattern>
              
          </servlet-mapping>
          posted on 2008-12-02 20:56 馬光軍 閱讀(78) 評(píng)論(0)  編輯  收藏

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 宁城县| 天气| 双流县| 太谷县| 独山县| 孝义市| 兰西县| 吉隆县| 宝坻区| 凤凰县| 新泰市| 师宗县| 广灵县| 利辛县| 上蔡县| 南充市| 彰武县| 昌黎县| 江西省| 通榆县| 修水县| 陈巴尔虎旗| 通州市| 怀仁县| 临邑县| 罗源县| 宣恩县| 本溪市| 永平县| 三门县| 梅州市| 灵丘县| 大田县| 江北区| 克山县| 凭祥市| 长宁区| 兴业县| 远安县| 扶余县| 新宾|