温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Flex2.0如何从零开始实现文件上传

发布时间:2021-11-24 13:05:50 来源:亿速云 阅读:117 作者:柒染 栏目:编程语言

这篇文章给大家介绍Flex2.0如何从零开始实现文件上传,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

Flex2.0 从零开始实现文件上传

以前在Flex1.5的时候也做过,不过当初使用的是oreilly的cos.jar。而且Flex1.5的时候在as里面无法直接引用FileReference类,只能写一个上传的as文件编译成swf文件,然后load这个swf文件来实现上传。

Flex2.0Release之后用oreilly的上传包做了一下上传,成功。于是回到apache的common-fileupload-1.1.1来研究上传。终于有了成果。再加上一直以来游走于各个论坛,发现好多工友对Flex2.0实现文件上传都很感兴趣。于是决定花一点时间将自己的成果跟大家分享一下。

1.环境的安装以及配置就不说了,网上很多地方可以找到。(我的是:JDK1.4.2,FlexBuilder2,Flex2SDK,Tomcat4.1,Eclips3.0.1,不过据说现在Flex2.0要使用RemoteObject的话需要安装JDK1.5)。

2.首先在Eclips中创建一个tomcat工程,例如取名为FileUpload。

3.找到FlexSDK安装目录,将flex.war拷贝出来更名为flex.rar。解开这个包。将里面的META-INF以及WEB-INF文件夹拷贝到Eclips的工作目录(我的是:d:workspaces)----即刚才创建的FileUpload目录下。

4.FlexBuilder2下创建一个新的工程。

5.工程中引入common-fileupload-1.1.1.jar以及common-io-1.2.jar(没有的话去http://www.apache.org下载)。

6.编写上传servletmyUpload.java代码如下(上传文件存放路径为:d:upload):

packagecom.fileupload;  importjava.io.File;  importjava.io.IOException;  importjava.util.Iterator;  importjava.util.List;importjavax.servlet.  ServletException;  importjavax.servlet.http.HttpServlet;  importjavax.servlet.http.HttpServletRequest;  importjavax.servlet.http.HttpServletResponse;  importorg.apache.commons.fileupload.FileItem;  importorg.apache.commons.fileupload.FileUploadException;  importorg.apache.commons.fileupload.disk.  DiskFileItemFactory;  importorg.apache.commons.fileupload.servlet.  ServletFileUpload;  publicclassmyUploadextendsHttpServlet{  privateStringuploadPath="D:\\upload\\";  privateintmaxPostSize=100*1024*1024;  publicvoiddoPost(HttpServletRequestreq,HttpServletResponseres)  throwsServletException,IOException{  res.setContentType("text/html;charset=UTF-8");   DiskFileItemFactoryfactory=newDiskFileItemFactory();  factory.setSizeThreshold(4096);  ServletFileUploadupload=newServletFileUpload(factory);  upload.setSizeMax(maxPostSize);  try{  ListfileItems=upload.parseRequest(req);  Iteratoriter=fileItems.iterator();  while(iter.hasNext()){  FileItemitem=(FileItem)iter.next();  if(!item.isFormField()){  Stringname=item.getName();  try{  item.write(newFile(uploadPath+name));  }catch(Exceptione){  e.printStackTrace();  }  }  }  }catch(FileUploadExceptione){  e.printStackTrace();  }  }  }

存放在../src/com/fileupload

7.在web.xml中加入如下代码。(用于调用servlet)

<servlet> <servlet-name>myUpload</servlet-name> <display-name>FileUploadServlet</display-name> <description>FileServletExample</description> <servlet-class>com.fileupload.myUpload</servlet-class> </servlet> <servlet-mapping> <servlet-name>myUpload</servlet-name> <url-pattern>/myUpload</url-pattern> </servlet-mapping>

8.前台的FileUpload.mxml文件代码如下:

<?xmlversionxmlversion="1.0"encoding="utf-8"?> <mx:Applicationxmlns:mxmx:Applicationxmlns:mx="http://www.adobe.com/2006/mxml" xmlns="*"creationComplete="init()"> <mx:Script> <![CDATA[  importflash.events.*;  importflash.net.FileReference;  importflash.net.URLRequest;  privatevarcurrentAction:String;  privatevaruploadURL:URLRequest;  privatevarfile:FileReference;   privatevarfileName:String;  privatefunctioninit():void{  file=newFileReference();  }   publicfunctionFileReference_browse():void{  currentAction="upload";  uploadURL=newURLRequest();  file=newFileReference();  configureListeners(file);  file.browse();  }  privatefunctionconfigureListeners(dispatcher:IEventDispatcher):void{  dispatcher.addEventListener(Event.SELECT,selectHandler);  }  privatefunctionselectHandler(event:Event):void{  varfile:FileReference=FileReference(event.target);  if(currentAction=="upload"){  uploadURL.url="myUpload?path=work&filename="+file.name;  file.upload(uploadURL);  }  }  ]]> </mx:Script> <mx:Panelwidthmx:Panelwidth="100%"height="100%"> <mx:VBoxwidthmx:VBoxwidth="100%"horizontalAlign="center"> <mx:Labeltextmx:Labeltext=  "Clickthebelowbuttontoselectafilewhichyouwanttoupload!"/> <mx:Buttonlabelmx:Buttonlabel="Upload"click="FileReference_browse()"/> </mx:VBox> </mx:Panel> </mx:Application>

9.开启tomcat,运行。大功告成!

关于Flex2.0如何从零开始实现文件上传就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI