[AS3]URLLoader+URLRequest+JPGEncoder实现BitmapData图片数据保存

    package project.test {  

          

        import flash.display.*;   

        import flash.net.*;  

        import flash.events.*;  

        import flash.utils.ByteArray;  

          

        import fl.controls.Button;  

          

        import com.klstudio.images.JPGEncoder;  

          

        public class Test12 extends Sprite{  

            private var _base:Shape;  

            private var _rnd_button:Button;  

            private var _submit_button:Button;  

              

            private static const UPLOAD_PAGE:String="http://localhost:8080/upload.jsp";   

              

            public function Test12(){             

                _base = new Shape();  

                _base.x = _base.y = 5;  

                addChild(_base);              

                drawLine();       

                  

                _rnd_button = new Button();  

                _rnd_button.label = "随机画图";  

                _rnd_button.setSize(90,22);  

                _rnd_button.move(5,205);  

                _rnd_button.addEventListener(MouseEvent.CLICK,clickHandler);  

                addChild(_rnd_button);  

                  

                _submit_button = new Button();  

                _submit_button.label = "保存图片";  

                _submit_button.setSize(90,22);  

                _submit_button.move(105,205);  

                _submit_button.addEventListener(MouseEvent.CLICK,clickHandler);  

                addChild(_submit_button);  

            }  

              

            public function drawRndRect():void{  

                _base.graphics.beginFill(getRndColor());  

                var gWidth:uint = getRndInt();  

                var gHeight:uint = getRndInt();  

                var gX:uint = getRndInt(5,180-gWidth+5);  

                var gY:uint = getRndInt(5,180-gHeight+5);  

                _base.graphics.drawRect(gX,gY,gWidth,gHeight);            

            }  

              

            public function getRndInt(min:uint=10,max:uint=180):uint{  

                return uint(Math.random()*(max-min))+min;  

            }  

              

            public function getRndColor():uint{  

                var r:uint = getRndInt(10,220);  

                var g:uint = getRndInt(10,220);  

                var b:uint = getRndInt(10,220);  

                var c:uint = (r << 16) + (g << 8) + b;  

                return c;  

            }  

              

            public function drawLine():void{              

                _base.graphics.beginFill(0x666666);  

                _base.graphics.drawRect(0,0,190,190);  

                _base.graphics.beginFill(0xFFFFFF);  

                _base.graphics.drawRect(1,1,188,188);  

                _base.graphics.endFill();  

            }  

              

            public function getBitmapData():BitmapData{  

                var bmd:BitmapData = new BitmapData(_base.width,_base.height);  

                bmd.draw(_base);  

                return bmd;  

            }  

              

            public function submit():void{  

                _rnd_button.enabled = false;  

                _submit_button.enabled = false;  

                var encoder:JPGEncoder = new JPGEncoder(80);  

                var bytes:ByteArray = encoder.encode(getBitmapData());  

                var request:URLRequest = new URLRequest(UPLOAD_PAGE);  

                //data值就为图片编码数据ByteArray;  

                request.data = bytes;  

                request.method = URLRequestMethod.POST;  

                //这个是关键,内容类型必须是下面文件流形式;  

                request.contentType = "application/octet-stream";  

                  

                var loader:URLLoader = new URLLoader();           

                loader.addEventListener(Event.COMPLETE, completeHandler);  

                loader.addEventListener(IOErrorEvent.IO_ERROR,errorHandler);  

                loader.load(request);  

            }  

              

            private function completeHandler(event:Event):void{  

                trace("图片上传成功");  

                _rnd_button.enabled = true;  

                _submit_button.enabled = true;  

            }  

              

            private function errorHandler(event:IOErrorEvent):void{  

                trace("图片上传失败");  

                _rnd_button.enabled = true;  

                _submit_button.enabled = true;  

            }  

              

            private function clickHandler(event:MouseEvent):void{  

                switch(event.target){  

                    case _rnd_button:  

                        _base.graphics.clear();  

                        drawLine();  

                        drawRndRect();  

                        drawRndRect();  

                        drawRndRect();  

                        drawRndRect();  

                        break;  

                    case _submit_button:  

                        submit();  

                        break;  

                }  

            }  

        }     

    }  

服务端代码(upload.jsp):我使用是Java服务端,当然PHP,ASP.NET也可以了,我在这里就不一一举例了,有兴趣的朋友自己研究了!

<%@ page contentType="text/html; charset=utf-8" language="java"%>  
<%@ page import="java.util.*"%>
<%@ page import="java.io.*"%>
<%
int v;
String filePath = request.getRealPath(System.currentTimeMillis()+".jpg");
BufferedInputStream inputStream = new BufferedInputStream(request.getInputStream());
FileOutputStream outputStream = new FileOutputStream(new File(filePath));
byte [] bytes = new byte[1024];
while((v=inputStream.read(bytes))>0){
outputStream.write(bytes,0,v);
}
outputStream.close();
inputStream.close();
%>



你可能感兴趣的:(request)