這篇文章給大家介紹Flex2.0如何從零開(kāi)始實(shí)現(xiàn)文件上傳,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。
創(chuàng)新互聯(lián)是一家專注于網(wǎng)站設(shè)計(jì)制作、成都網(wǎng)站制作與策劃設(shè)計(jì),張北網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設(shè)十多年,網(wǎng)設(shè)計(jì)領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:張北等地區(qū)。張北做網(wǎng)站價(jià)格咨詢:18980820575
Flex2.0 從零開(kāi)始實(shí)現(xiàn)文件上傳
以前在Flex1.5的時(shí)候也做過(guò),不過(guò)當(dāng)初使用的是oreilly的cos.jar。而且Flex1.5的時(shí)候在as里面無(wú)法直接引用FileReference類,只能寫(xiě)一個(gè)上傳的as文件編譯成swf文件,然后load這個(gè)swf文件來(lái)實(shí)現(xiàn)上傳。
Flex2.0Release之后用oreilly的上傳包做了一下上傳,成功。于是回到apache的common-fileupload-1.1.1來(lái)研究上傳。終于有了成果。再加上一直以來(lái)游走于各個(gè)論壇,發(fā)現(xiàn)好多工友對(duì)Flex2.0實(shí)現(xiàn)文件上傳都很感興趣。于是決定花一點(diǎn)時(shí)間將自己的成果跟大家分享一下。
1.環(huán)境的安裝以及配置就不說(shuō)了,網(wǎng)上很多地方可以找到。(我的是:JDK1.4.2,F(xiàn)lexBuilder2,F(xiàn)lex2SDK,Tomcat4.1,Eclips3.0.1,不過(guò)據(jù)說(shuō)現(xiàn)在Flex2.0要使用RemoteObject的話需要安裝JDK1.5)。
2.首先在Eclips中創(chuàng)建一個(gè)tomcat工程,例如取名為FileUpload。
3.找到FlexSDK安裝目錄,將flex.war拷貝出來(lái)更名為flex.rar。解開(kāi)這個(gè)包。將里面的META-INF以及WEB-INF文件夾拷貝到Eclips的工作目錄(我的是:d:workspaces)----即剛才創(chuàng)建的FileUpload目錄下。
4.FlexBuilder2下創(chuàng)建一個(gè)新的工程。
5.工程中引入common-fileupload-1.1.1.jar以及common-io-1.2.jar(沒(méi)有的話去http://www.apache.org下載)。
6.編寫(xiě)上傳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.前臺(tái)的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.開(kāi)啟tomcat,運(yùn)行。大功告成!
關(guān)于Flex2.0如何從零開(kāi)始實(shí)現(xiàn)文件上傳就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。
網(wǎng)頁(yè)題目:Flex2.0如何從零開(kāi)始實(shí)現(xiàn)文件上傳
URL網(wǎng)址:http://chinadenli.net/article2/ihjooc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站建設(shè)、網(wǎng)站導(dǎo)航、網(wǎng)站維護(hù)、網(wǎng)站策劃、App開(kāi)發(fā)、小程序開(kāi)發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)