注意点:
1.FileReference.download() 方法提示用户提供文件的保存位置并开始从远程 URL 进行下载。直接加载请求路径下载,不需要后台的支持。
2.针对文件中文名的问题,需要双方设置编码:
首先flex端:
var download_request:URLRequest=new URLRequest(encodeURI(StringUtil.trim(url)));
- encodeURI(uri:String="undefined"):String
- 将字符串编码为有效的 URI(统一资源标识符)。将完整的 URI 转换为一个字符串,其中除属于一小组基本字符的字符外,其他所有字符都以 UTF-8 转义序列进行编码。
- 然后tomcat端:
- 打开 \apache-tomcat-7.0.39\conf\server.xml配置文件,找到下面配置:
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" URIEncoding="UTF-8"/>
修改或是追加红色部分内容
然后重启服务。
具体Flex下载端代码如下:
fileDownLoad.mxml
<?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"> <fx:Script> <![CDATA[ import mx.controls.Alert; import mx.utils.StringUtil; protected function button1_clickHandler(event:MouseEvent):void { // TODO Auto-generated method stub downLoad("http://192.168.191.5:8080/netcanvas_s/","360软件小助手截图20140508200748.png"); } private var fileRef:FileReference=new FileReference(); /** * 文件下载 * backUrl: 后台服务器地址 * pathFileName: 下载的文件路径+文件名 * */ public function downLoad(backUrl:String,pathFileName:String):void{ fileRef.addEventListener(Event.COMPLETE,completeHandler ); fileRef.addEventListener(SecurityErrorEvent.SECURITY_ERROR,securityErrorHandler); fileRef.addEventListener(ProgressEvent.PROGRESS, progressHandler); fileRef.addEventListener(IOErrorEvent.IO_ERROR,ioShow); var fileNameArr:Array=StringUtil.trim(pathFileName).split(‘/‘); var decodeName=fileNameArr[fileNameArr.length-1]; //截取文件名 var url:String=backUrl+pathFileName; var download_request:URLRequest=new URLRequest(encodeURI(StringUtil.trim(url))); fileRef.download(download_request,decodeName); //下载文件并加入默认文件名 } //下载进入处理时间 private function progressHandler(event:ProgressEvent):void{ lbProgress.text = " 已下载 " + (event.bytesLoaded/1024).toFixed(2)+ " K,共 " + (event.bytesTotal/1024).toFixed(2) + " K"; var proc: uint = event.bytesLoaded / event.bytesTotal * 100; progress1.setProgress(proc, 100); progress1.label= "当前进度: " + " " + proc + "%"; } //文件下载成功事件 private function completeHandler(event:Event):void { mx.controls.Alert.show("文件下载成功"); } //I/O错误处理 private function ioShow(evt: IOErrorEvent){ Alert.show(evt.toString(),"IO错误"); } //安全沙箱问题事件 private function securityErrorHandler(event:SecurityErrorEvent){ Alert.show(event.text); } ]]> </fx:Script> <fx:Declarations> <!-- 将非可视元素(例如服务、值对象)放在此处 --> </fx:Declarations> <s:Button x="289" y="149" label="下载" click="button1_clickHandler(event)"/> <mx:ProgressBar x="10" y="40" width="457" minimum="0" mode="manual" maximum="100" id="progress1" label="当前进度: 0%" styleName="myfont" fontWeight="normal"/> <mx:Label x="146" y="98" width="321" id="lbProgress" styleName="myfont" textAlign="right"/> </s:Application>
显示效果如图:
flex 文件下载 +Tomcat web应用服务器,布布扣,bubuko.com
时间: 2024-08-10 19:15:53