首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 媒体动画 > flex >

flex struts2下传文件

2012-09-09 
flex struts2上传文件?xml version1.0 encodingutf-8?mx:Application xmlns:mxhttp://www.adob

flex struts2上传文件
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
                layout="absolute" xmlns="*" creationComplete="init();">
<mx:Script>
    <![CDATA[
        import flash.net.FileReference;
        import mx.controls.Alert;
        import mx.events.CloseEvent;
        import flash.events.*;
       
        private var file: FileReference;
       
        private function init(): void
        {
            Security.allowDomain("*");
            file = new FileReference();
            file.addEventListener(ProgressEvent.PROGRESS, onProgress);
            file.addEventListener(Event.SELECT, onSelect);
            file.addEventListener(Event.COMPLETE, completeHandle);
        }
       
        private function completeHandle(event:Event):void{
            Alert.show("恭喜你,上传成功");
        }
       
        private function upload(): void
        {
            var imageTypes:FileFilter = new FileFilter("Videos (*.mov,*.rmvb,*.flv)", "*.mov;*.rmvb;*.flv");
            var allTypes:Array = new Array(imageTypes);
            file.browse(allTypes);
            file.browse();
        }
        private function onSelect(e: Event): void
        {
            Alert.show("上传 " + file.name + " (共 "+Math.round(file.size)+" 字节)?",
            "确认上传",
            Alert.YES|Alert.NO,
            null,
            proceedWithUpload);
        }
       
        private function onProgress(e: ProgressEvent): void
        {
            lbProgress.text = " 已上传 " + e.bytesLoaded
            + " 字节,共 " + e.bytesTotal + " 字节";
            var proc: uint = e.bytesLoaded / e.bytesTotal * 100;
            bar.setProgress(proc, 100);
            bar.label= "当前进度: " + " " + proc + "%";
        }
       
        private function proceedWithUpload(e: CloseEvent): void
        {
            if (e.detail == Alert.YES)
            {
                var request: URLRequest = new URLRequest("FileUpload");
                request.contentType="multipart/form-data";
                try {
                    file.upload(request);
                }
                catch (error:Error)
                {
                    trace("上传失败");
                }
       
            }
        }
    ]]>
    </mx:Script>

    <mx:Canvas width="100%" height="100%" x="10" y="170" fontSize="15">
        <mx:VBox width="100%" horizontalAlign="center">
            <mx:Label id="lbProgress" text="上传"/>
            <mx:ProgressBar id="bar" labelPlacement="bottom"
                        minimum="0" visible="true" maximum="100" label="当前进度: 0%"
                            direction="right" mode="manual" width="200"/>
       
            <mx:Button label="上传文件" click="upload();"/>
        </mx:VBox>
    </mx:Canvas>
</mx:Application>




这个貌似网上很多的,不多说了的。




然后是比较关键的action文件:

public String flexUpLoadFile()
    {
        MultiPartRequestWrapper multiWrapper =
            (MultiPartRequestWrapper) ServletActionContext.getRequest();
        Enumeration fileParameterNames = multiWrapper.getFileParameterNames();
        while (fileParameterNames != null && fileParameterNames.hasMoreElements())
        {    
            // get the value of this input tag    
            String inputName = (String) fileParameterNames.nextElement();    
   
            // get the content type    
            String[] contentType = multiWrapper.getContentTypes(inputName);    
   
            if (isNonEmpty(contentType)) {    
                // get the name of the file from the input tag    
                String[] fileName = multiWrapper.getFileNames(inputName);    
   
                if (isNonEmpty(fileName)) {    
                    // Get a File object for the uploaded File    
                    File[] files = multiWrapper.getFiles(inputName);    
                    if (files != null) {    
                        for (int index = 0; index < files.length; index++) {    
                            String uploadPath = "D:\"+fileName[index];    
                            File dstFile = new File(uploadPath);    
                            this.copy(files[index], dstFile);    
                        }    
                    }    
                }     
            }    
        }    
        return null;
    }











    private void copy(File src, File dst) {       
        InputStream in = null;       
        OutputStream out = null;    
   
        try {       
            in = new BufferedInputStream(new FileInputStream(src), UserUtil.BUFFER_SIZE);       
            out = new BufferedOutputStream(new FileOutputStream(dst),       
                    UserUtil.BUFFER_SIZE);       
            byte[] buffer = new byte[UserUtil.BUFFER_SIZE];       
            int len = 0;       
            while ((len = in.read(buffer)) > 0) {       
                out.write(buffer, 0, len);       
            }       
        } catch (Exception e) {       
            e.printStackTrace();       
        } finally {       
            if (null != in) {       
                try {       
                    in.close();       
                } catch (IOException e) {       
                    e.printStackTrace();       
                }       
            }       
            if (null != out) {       
                try {       
                    out.close();       
                } catch (IOException e) {       
                    e.printStackTrace();       
                }       
            }       
        }       
    }
   
    private static boolean isNonEmpty(Object[] objArray) {    
        boolean result = false;    
        for (int index = 0; index < objArray.length && !result; index++) {    
            if (objArray[index] != null) {    
                result = true;    
            }    
        }    
        return result;    
    }



来自:http://blog.csdn.net/shangdyu/article/details/7005540


没测试过。


struts.xml部分:


<action name="FileUpload" method="flexUpLoadFile">
            <result>/UpLoad.jsp</result>
</action>


flexUpLoadFile这个函数貌似比较关键,我试过很多的版本都不行,这边可能是这个函数起的作用,先记录下来吧。

MultiPartRequestWrapper multiWrapper =
            (MultiPartRequestWrapper) ServletActionContext.getRequest();
        Enumeration fileParameterNames = multiWrapper.getFileParameterNames();

它接收到了从flex传过来的文件的内容。

热点排行