Flex4+blazeDS 上传文件(进度条)

<?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>
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值