<?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" currentState="pre_upload" creationComplete="init()"> <s:layout> <s:BasicLayout/> </s:layout> <fx:Script> <![CDATA[ //import mx.events.ImageUploadedEvent; import flash.events.Event; import flash.net.FileFilter; import flash.net.FileReference; import mx.controls.Alert; import mx.managers.PopUpManager; import mx.rpc.events.FaultEvent; import mx.rpc.events.ResultEvent; import mx.rpc.remoting.mxml.RemoteObject; private var fileRef:FileReference = new FileReference(); private function init():void{ } private function pickFile(evt:MouseEvent):void { var imageTypes:FileFilter = new FileFilter("图片 (*.jpg, *.jpeg, *.gif,*.png)", "*.jpg; *.jpeg; *.gif; *.png"); var allTypes:Array = new Array(imageTypes); fileRef.addEventListener(Event.SELECT, selectHandler); fileRef.addEventListener(ProgressEvent.PROGRESS, progressHandler); fileRef.addEventListener(Event.COMPLETE, completeHandler); try{ fileRef.browse(allTypes); }catch (error:Error){ trace("Unable to browse for files."+error.toString()); } } private function progressHandler(evt:ProgressEvent):void { lb_progress.text = " 已上传 " + (evt.bytesLoaded/1024).toFixed(2)+ " K,共 " + (evt.bytesTotal/1024).toFixed(2) + " K"; var proc: uint = evt.bytesLoaded / evt.bytesTotal * 100; progress.setProgress(proc, 100); progress.label= "当前进度: " + " " + proc + "%"; } private function selectHandler(evt:Event):void { currentState = "uploading"; fileRef.load(); } private function completeHandler(evt:Event):void{ currentState = "post_upload"; upload.uploadFile(fileRef.data,fileRef.name); image_post_upload.source = fileRef.data; } private function abc(e:ResultEvent):void{ Alert.show("success"); } ]]> </fx:Script> <fx:Declarations> <s:RemoteObject id="upload" destination="UploadFile" endpoint="/messagebroker/amf"> <s:method name="uploadFile" result="abc(event)"/> </s:RemoteObject> </fx:Declarations> <s:states> <s:State name = "pre_upload"/> <s:State name = "uploading"/> <s:State name = "post_upload"/> </s:states> <s:Button includeIn="pre_upload" label="从电脑上上传图片" width="119" click="pickFile(event)" horizontalCenter="-1" y="29"/> <mx:ProgressBar id="progress" includeIn="uploading" x="43" y="43" width="153"/> <s:Label id="lb_progress" includeIn="uploading" x="45" y="10" width="149" height="25"/> <s:Label id="lb_post_upload" includeIn="post_upload" x="108" y="21" width="124" height="32" fontSize="16" text="upload success!" fontFamily="Arial" color="#3374DE"/> <mx:Image id="image_post_upload" includeIn="post_upload" x="10" y="10" width="67" height="67"/> </s:Application>
这是java文件,实现文件的上传
import java.io.FileOutputStream;
public class UploadFile {
public void uploadFile(byte[] content, String fileName) throws Exception {
File file = new File("c:\\" + fileName);
FileOutputStream stream = new FileOutputStream(file);
if (content != null)
stream.write(content);
stream.close();
}
}
然后修改 remoting-service.xml文件
<service id="remoting-service" class="flex.messaging.services.RemotingService"> <adapters> <adapter-definition id="java-object" class="flex.messaging.services.remoting.adapters.JavaAdapter" default="true" /> </adapters> <default-channels> <channel ref="my-amf" /> </default-channels> <destination id="UploadFile"> <properties> <source>com.upload.UploadFile</source> </properties> </destination> </service>