Flex2.0如何從零開始實(shí)現(xiàn)文件上傳

這篇文章給大家介紹Flex2.0如何從零開始實(shí)現(xiàn)文件上傳,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

創(chuàng)新互聯(lián)是一家專注于網(wǎng)站設(shè)計制作、成都網(wǎng)站制作與策劃設(shè)計,張北網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設(shè)十多年,網(wǎng)設(shè)計領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:張北等地區(qū)。張北做網(wǎng)站價格咨詢:18980820575

Flex2.0 從零開始實(shí)現(xiàn)文件上傳

以前在Flex1.5的時候也做過,不過當(dāng)初使用的是oreilly的cos.jar。而且Flex1.5的時候在as里面無法直接引用FileReference類,只能寫一個上傳的as文件編譯成swf文件,然后load這個swf文件來實(shí)現(xiàn)上傳。

Flex2.0Release之后用oreilly的上傳包做了一下上傳,成功。于是回到apache的common-fileupload-1.1.1來研究上傳。終于有了成果。再加上一直以來游走于各個論壇,發(fā)現(xiàn)好多工友對Flex2.0實(shí)現(xiàn)文件上傳都很感興趣。于是決定花一點(diǎn)時間將自己的成果跟大家分享一下。

1.環(huán)境的安裝以及配置就不說了,網(wǎng)上很多地方可以找到。(我的是:JDK1.4.2,F(xiàn)lexBuilder2,F(xiàn)lex2SDK,Tomcat4.1,Eclips3.0.1,不過據(jù)說現(xiàn)在Flex2.0要使用RemoteObject的話需要安裝JDK1.5)。

2.首先在Eclips中創(chuàng)建一個tomcat工程,例如取名為FileUpload。

3.找到FlexSDK安裝目錄,將flex.war拷貝出來更名為flex.rar。解開這個包。將里面的META-INF以及WEB-INF文件夾拷貝到Eclips的工作目錄(我的是:d:workspaces)----即剛才創(chuàng)建的FileUpload目錄下。

4.FlexBuilder2下創(chuàng)建一個新的工程。

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中加入如下代碼。(用于調(diào)用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,運(yùn)行。大功告成!

關(guān)于Flex2.0如何從零開始實(shí)現(xiàn)文件上傳就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

網(wǎng)頁題目:Flex2.0如何從零開始實(shí)現(xiàn)文件上傳
URL網(wǎng)址:http://muchs.cn/article2/ihjooc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站建設(shè)、網(wǎng)站導(dǎo)航網(wǎng)站維護(hù)、網(wǎng)站策劃App開發(fā)、小程序開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作