Flex和Servlet结合上传文件(二)

2014-11-23 22:32:15 · 作者: · 浏览: 1
(file + "\\" + fileName); try { FileOutputStream fileOutSt = new FileOutputStream(fileFolderName); try { fileOutSt.write(data); fileOutSt.close(); } catch (IOException exception) { exception.printStackTrace(); } } catch (FileNotFoundException exception) { exception.printStackTrace(); } } } } catch (FileUploadException exception) { exception.printStackTrace(); } } /** * Initialization of the servlet.
* * @throws ServletException * if an error occurs */ public void init() throws ServletException { // Put your code here } } (3)配置web.xml

  

  
  
   
  
    
    
     This is the description of my J2EE component
     
    
     This is the display name of my J2EE component
     
    
     FlexFileUploadServlet
     
    
     com.you.upload.servlet.FlexFileUploadServlet
     
     
     
      上传文件临时路径
      
     
      file
      
     
      file
      
     
   

  
    
    
     FlexFileUploadServlet
     
    
     /FlexFileUploadServlet
     
   	
  
    
    
     index.
     jsp
     
   

  

(4)打开Flash Builder,新建Flex项目

FileUpload.mxml:

  

  
	
   
     @namespace s "library://ns.adobe.com/flex/spark"; @namespace mx "library://ns.adobe.com/flex/mx"; Panel { borderStyle: solid; borderColor: #ff0000; borderAlpha: 0.5; borderThickness: 2; roundedBottomCorners: true; cornerRadius: 10; headerHeight: 50; backgroundAlpha: 0.5; highlightAlphas: 0.4, 0.24; headerColors: #cccc00, #cccc00; footerColors: #660033, #4217c2; backgroundColor: #00ffff; dropShadowEnabled: true; shadowDistance: 1; titleStyleName: "mypanelTitle"; } .mypanelTitle { letterSpacing: 1; color: #6600cc; textAlign: left; fontFamily: Trebuchet MS; fontSize: 16; fontWeight: bold; fontStyle: italic; textDecoration: underline; } 
   
	
   
     import mx.collections.ArrayCollection; import mx.controls.Alert; import mx.events.FlexEvent; [Bindable] //提示用户选择要上载的文件或用于下载的位置 private var fileRefer:FileReference; [Bindable] //判断文件是否选中 private var fileSelected:Boolean; [Bindable] //上传文件临时路径 private var serverURL:String = "http://localhost:8686/FlexFileUpload/FlexFileUploadServlet"; private var stat:Array = new Array(); [Bindable] private var statusArray:ArrayCollection = new ArrayCollection(); /** * 初始化函数 */ protected function initHandler():void { fileRefer = new FileReference(); fileRefer.addEventListener(Event.SELECT, onFileSelect); fileRefer.addEventListener(ProgressEvent.PROGRESS,onUploadProgress); fileRefer.addEventListener(Event.COMPLETE, onUploadComplete); fileRefer.addEventListener(IOErrorEvent.IO_ERROR, onUploadError); fileRefer.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onUploadError); } /** * 选择文件事件函数 */ private function onFileSelect(event:Event):void { fileSelected = true; fileTxt.text = fileRefer.name; stat.push({status:"准备上传 "+fileTxt.text}); statusArray = new ArrayCollection(stat); } /** * 正在上传事件函数 */ private function onUploadProgress(event:ProgressEvent):void { stat.push({status:"进度.."+((event.bytesLoaded * 100) / event.bytesTotal).toString()+"%"}); statusArray = new ArrayCollection(stat); } /** * 上传完全事件函数 */ private function onUploadComplete(event:Event):void { stat.push({status:"上传成功!"}); statusArray = new ArrayCollection(stat); } /** * 上传出现错误事件函数 */ private function onUploadError(event:Event):void { if (event is IOErrorEvent) { stat.push({status:"输入输出错误: "+(event as IOErrorEvent).text.toStr