flex获取基于WebService返回的验证码(Java)

近段时间做用户登录,用到验证码,开始是用客户端生成的
后来知道客户端生成的种种弊端,所以还是用服务端
但是以前有没有做过,到网上查了一下,有一个例子,不过是ASP.NET的
自己是搞JAVA的,所以就想写一个,说不上是原创,只不过是把各种资料
整合起来,献给大家
VerifyCode.mxml
<?xml version="1.0" encoding="utf-8"?>
<mx:Image xmlns:mx="http://www.adobe.com/2006/mxml" 
buttonMode="true" mouseChildren="false"
   creationComplete="creationCompleteHandler()"
click="imageClickHandler(event)">
   <mx:Script>
       <![CDATA[
           import mx.core.UIComponent;
           import mx.rpc.events.FaultEvent;
           import mx.controls.Alert;
           import mx.rpc.events.ResultEvent;
           import flash.display.Loader;

           import mx.rpc.soap.mxml.WebService;
          
           private var _WSDLString:String;
           public function set WSDLString(WSDL_String:String):void{
               this._WSDLString = WSDL_String;
           }
          
           private function creationCompleteHandler():void{
               callWebService();
           }
          
           private function imageClickHandler(evt:MouseEvent):void{
               callWebService();
           }
          
           private var ws:WebService;
           public function callWebService():void{
               ws = new WebService();
               ws.showBusyCursor = true;
               ws.loadWSDL(_WSDLString);
               ws.addEventListener(ResultEvent.RESULT,resultHandler);
               ws.addEventListener(FaultEvent.FAULT,faultHandler);
               ws.imageToByteArray();
           }
           public var returnedVerifyCode:String;
           private function resultHandler(evt:ResultEvent):void{
               var ba:ByteArray = (evt.result.verifyCodeImage) as ByteArray; 
               returnedVerifyCode = evt.result.verifyCodeString as String;
               var loader:Loader = new Loader();
               loader.loadBytes(ba);
               var uiComponent:UIComponent = new UIComponent();
               uiComponent.addChild(loader);
               this.addChild(uiComponent);
           }
          
           public function getVerifyCode():String{
               return returnedVerifyCode;
           }
          
           private function faultHandler(evt:FaultEvent):void{
               Alert.show(evt.fault.faultDetail+"","提示");
           }
           private function removeEventListeners():void{
               ws.removeEventListener(ResultEvent.RESULT,resultHandler);
               ws.removeEventListener(FaultEvent.FAULT,faultHandler);
           }
       ]]>
   </mx:Script><mx:WebService/>
</mx:Image>


import java.util.Random;
import java.awt.image.BufferedImage;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Font;
import java.io.ByteArrayOutputStream;

import javax.imageio.ImageIO;

import org.xiongbo.webservice.VerifyCodeBean;

public class MyWebService {

   public MyWebService(){
      
   }
  
   public VerifyCodeBean imageToByteArray(){
      
       String chose = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

       char display[] = { '0', ' ', '0', ' ', '0', ' ', '0' };
       char ran[] = {'0', '0', '0', '0' };
       char temp;

       Random rand = new Random();

       for (int i = 0; i < 4; i++) {

           temp = chose.charAt(rand.nextInt(chose.length()));

           display[i * 2] = temp;

           ran[i] = temp;
       }

       String random = String.valueOf(display);
      
       int width = 75, height = 25;
       BufferedImage image = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
       Graphics g = image.getGraphics();
       //以下填充背景颜色
       g.setColor(Color.GREEN);
       g.fillRect(0, 0, width, height);
       //设置字体颜色
       g.setColor(Color.RED);
       Font font = new Font("Arial", Font.PLAIN, 18);
       g.setFont(font);
       g.drawString(random, 5, 20);
       //产生随机线条
       for (int i = 0; i < 30; i++) {
           int x = rand.nextInt(width - 1);
           int y = rand.nextInt(height - 1);
           int x1 = rand.nextInt(6) + 1;
           int y1 = rand.nextInt(12) + 1;
           g.drawLine(x, y, x + x1, y + y1);
       }
      
       g.dispose();
      
       ByteArrayOutputStream out = new ByteArrayOutputStream(); 
      
       try {
           ImageIO.write(image, "PNG", out);
       } catch (Exception e) {
           e.printStackTrace();
       }   

       //去掉验证码中的空格
       String [] interceptString = random.split(" ");
       String returnVerifyCode = "";
       for(int i = 0;i<interceptString.length;i++){
           returnVerifyCode += interceptString[i];
       }

       //把多个返回值保存到对象并返回到客户端
       VerifyCodeBean obj = new VerifyCodeBean();
       obj.setVerifyCodeString(returnVerifyCode);
       obj.setVerifyCodeImage(out.toByteArray());   
      
       return obj;
   }
}
 
转自 http://hi.baidu.com/bosshung

你可能感兴趣的:(java,webservice,Flex,asp.net,asp)