• [ActionScript 3.0] AS向php发送二进制数据方法之——在URLRequest中构造HTTP协议发送数据


    主类 HTTPSendPHP.as

     1 package 
     2 {
     3     import com.JPEGEncoder.JPGEncoder;
     4     import com.fylib.httpRequest.HttpRequestBuilder;
     5     import com.fylib.httpRequest.HttpRequestBuilderConsts;
     6     import flash.display.Bitmap;
     7     import flash.display.BitmapData;
     8     import flash.display.Loader;
     9     import flash.display.Sprite;
    10     import flash.events.Event;
    11     import flash.events.HTTPStatusEvent;
    12     import flash.net.URLLoader;
    13     import flash.net.URLRequest;
    14     import flash.utils.ByteArray;
    15 
    16     /**
    17       * ...
    18       * @author Frost.Yen
    19       */
    20     public class HTTPSendPHP extends Sprite
    21     {
    22         private var bmp:Bitmap;
    23         public function HTTPSendPHP()
    24         {
    25             var ldr:Loader = new Loader  ;
    26             ldr.load(new URLRequest("image.jpg"));
    27             //加载的图片;
    28             ldr.contentLoaderInfo.addEventListener(Event.COMPLETE,onComplete);
    29         }
    30 
    31         private function onComplete(e:Event):void
    32         {
    33             var bmpd:BitmapData = e.target.content as Bitmap.bitmapData;
    34             var jpgEncoder:JPGEncoder = new JPGEncoder(80);//图片质量
    35             var jpgStream:ByteArray = jpgEncoder.encode(bmpd);//将图片数据转换成ByteArray存储
    36 
    37             var HttpR:HttpRequestBuilder = new HttpRequestBuilder("php端地址");//php端地址
    38             HttpR.writeImage("图片变量","图片名称",HttpRequestBuilderConsts.JPG,jpgStream);
    39             //图片变量如:"data[Image][name]";
    40             HttpR.writeHttpEnd();
    41             HttpR.writeSeparator();
    42 
    43             var loader:URLLoader = new URLLoader  ;
    44             var request:URLRequest = HttpR.getURLrequest();
    45             loader.load(request);
    46             loader.addEventListener(HTTPStatusEvent.HTTP_STATUS,httpStatusHandler);
    47             loader.addEventListener(Event.COMPLETE,onSendComplete);
    48         }
    49 
    50         private function onSendComplete(e:Event):void
    51         {
    52             trace("complete");
    53         }
    54 
    55         private function httpStatusHandler(e:HTTPStatusEvent):void
    56         {
    57             trace(("status is " + e.status));
    58         }
    59     }
    60 
    61 }

    其他类

      1 package com.fylib.httpRequest
      2 {
      3     import flash.net.URLRequest;
      4     import flash.net.URLRequestMethod;
      5     import flash.net.URLVariables;
      6     import flash.utils.ByteArray;
      7 
      8     /**
      9       * ...
     10       * @author 
     11       */
     12     public class HttpRequestBuilder
     13     {
     14         /**
     15           * 构造的URLRequest
     16           */
     17         private var _request:URLRequest;
     18         /**
     19           * 构造的二进制ByteArray
     20           */
     21         private var _byteArray:ByteArray;
     22         /**
     23           * Http协议分割符
     24           */
     25         private var _separator:String;
     26         /**
     27           * 标志位  是否填写HTTP文件尾
     28           */
     29         private var PROTOCAL_END_SET:Boolean = false;
     30 
     31         public function HttpRequestBuilder($url:String,$separator:String="7d86d710144a")
     32         {
     33             //初始化
     34             this._separator = $separator;
     35             this._request = new URLRequest($url);
     36             this._request.method = URLRequestMethod.POST;
     37             this._request.contentType = "multipart/form-data; boundary=---------------------------" + this._separator;
     38             this._byteArray = new ByteArray  ;
     39         }
     40 
     41         /*
     42          * 写入变量
     43          * @param $name 写入的变量名
     44          * @param $value 写入的变量值
     45          */
     46         public function writeVariable($name:String,$value:String):void
     47         {
     48             writeSeparator();
     49             _byteArray.writeUTFBytes(((("Content-Disposition: form-data; name="" + $name) + ""
    
    ") + $value));
     50         }
     51 
     52         /*
     53          * 写入图片
     54          * @param $name 变量名
     55          * @param $filename 图片文件名
     56          * @param $type 图片类型,在HttpRequestBuilderConsts下定义
     57          * @param $content 图片二进制数据
     58          */
     59         public function writeImage($name:String,$filename:String,$type:String,$content:ByteArray):void
     60         {
     61             writeSeparator();
     62             _byteArray.writeUTFBytes((((((("Content-Disposition: form-data; name="" + $name) + ""; filename="") + $filename) + ""
    Content-Type: ") + $type) + "
    
    "));
     63             _byteArray.writeBytes($content);
     64         }
     65 
     66         /*
     67          * 构造HTTP分割线
     68          */
     69         public function writeSeparator():void
     70         {
     71             _byteArray.writeUTFBytes((("
    -----------------------------" + separator) + "
    "));
     72         }
     73 
     74         /*
     75          * 构造HTTP结尾,只能调用一次,二次调用会抛出错误
     76          */
     77         public function writeHttpEnd():void
     78         {
     79             if (! PROTOCAL_END_SET)
     80             {
     81                 _byteArray.writeUTFBytes((("
    -----------------------------" + separator) + "--"));
     82                 PROTOCAL_END_SET = true;
     83             }
     84             else
     85             {
     86                 throw new Error("Write the Http End More Than Once");
     87             }
     88         }
     89 
     90         /*
     91          * 获取构造好的URLRequest
     92          */
     93         public function getURLrequest():URLRequest
     94         {
     95             return this.request;
     96         }
     97 
     98         //get set
     99         public function get separator():String
    100         {
    101             return _separator;
    102         }
    103         /*public function set separator(value:String):void
    104           {
    105            //TODO 替换之前写入的内容
    106            _separator = value;
    107           }*/
    108 
    109         /*
    110          * 获取前会检查是否写入HTTP结尾,未调用的话会报错
    111          */
    112         public function get request():URLRequest
    113         {
    114             if (! PROTOCAL_END_SET)
    115             {
    116                 throw new Error("Havn't Write the Http End Yet");
    117             }//??还是应该自动构造
    118             _request.data = _byteArray;
    119             return _request;
    120         }
    121     }
    122 
    123 }
     1 package com.fylib.httpRequest
     2 {
     3     /**
     4       * ...
     5       * @author 
     6       */
     7     public class HttpRequestBuilderConsts
     8     {
     9         public static const JPG:String = "image/jpeg";
    10         public static const PNG:String = "image/x-png";
    11 
    12         public function HttpRequestBuilderConsts()
    13         {
    14 
    15         }
    16 
    17     }
    18 
    19 }
  • 相关阅读:
    log4j2 配置详解
    MANIFEST.MF文件详解
    assembly.xml
    firewall 和 iptables 常用命令
    Spring boot 使用 configuration 获取的属性为 null
    使用 maven-assembly-plugin 打包项目
    使用 Maven 插件将 class(字节码文件),resource(资源文件),lib(依赖的jar包)分开打包
    HttpClient 传输文件的两种方式
    IDEA中读取 resource目录下文件
    3.2、Android Studio在物理设备中运行APP
  • 原文地址:https://www.cnblogs.com/frost-yen/p/4544108.html
Copyright © 2020-2023  润新知