加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

在网上找的一个Flex文件上传代码,记录下来,以后用到的时候可以

发布时间:2020-12-15 05:17:28 所属栏目:百科 来源:网络整理
导读:? 在网上找的一个Flex文件上传代码,记录下来,以后用到的时候可以马上能用: 1. Flex 前端代码 [java] view plain copy print ? ?xml?version= "1.0" ?encoding= "utf-8" ??? s:Application?xmlns:fx= "http://ns.adobe.com/mxml/2009" ?? ??????xmlns:s= "
?

在网上找的一个Flex文件上传代码,记录下来,以后用到的时候可以马上能用:

1. Flex 前端代码

[java] view plain copy print ?
  1. <?xml?version="1.0"?encoding="utf-8"?>??
  2. <s:Application?xmlns:fx="http://ns.adobe.com/mxml/2009"??
  3. ??????xmlns:s="library://ns.adobe.com/flex/spark"??
  4. ??????xmlns:mx="library://ns.adobe.com/flex/mx"?minWidth="955"?minHeight="600"??
  5. ??????creationComplete="init();">??
  6. ?<s:layout>??
  7. ??<s:BasicLayout/>??
  8. ?</s:layout>??
  9. ?<fx:Declarations>??
  10. ??<!--?将非可视元素(例如服务、值对象)放在此处?-->??
  11. ?</fx:Declarations>??
  12. ?<fx:Script>??
  13. ??<![CDATA[??
  14. ??import?flash.net.FileReference;??
  15. ??import?mx.controls.Alert;??
  16. ??import?mx.events.CloseEvent;??
  17. ??import?flash.events.*;??
  18. ????
  19. ??private?var?file:?FileReference;??
  20. ????
  21. ??private?function?init():?void{??
  22. ??Security.allowDomain("*");??
  23. ??file?=?new?FileReference();??
  24. ??file.addEventListener(ProgressEvent.PROGRESS,?onProgress);??
  25. ??file.addEventListener(Event.SELECT,?onSelect);??
  26. ??}??
  27. ????
  28. ??private?function?upload():?void{??
  29. ??????file.browse();??
  30. ??}??
  31. ??private?function?onSelect(e:?Event):?void{??
  32. ??Alert.show("上传?"?+?file.name?+?"?(共?"+Math.round(file.size)+"?字节)?",??
  33. ??"确认上传",??
  34. ??Alert.YES|Alert.NO,??
  35. ??null,??
  36. ??proceedWithUpload);??
  37. ??}??
  38. ????
  39. ??private?function?onProgress(e:?ProgressEvent):?void{??
  40. ??lbProgress.text?=?"?已上传?"?+?e.bytesLoaded??
  41. ??+?"?字节,共?"?+?e.bytesTotal?+?"?字节";??
  42. ??var?proc:?uint?=?e.bytesLoaded?/?e.bytesTotal?*?100;??
  43. ??bar.setProgress(proc,?100);??
  44. ??bar.label=?"当前进度:?"?+?"?"?+?proc?+?"%";??
  45. ??}??
  46. ????
  47. ??private?function?proceedWithUpload(e:?CloseEvent):?void{??
  48. ??if?(e.detail?==?Alert.YES){??
  49. ??var?request:?URLRequest?=?new?URLRequest("http://localhost:8080/firstFlex/fileUploadServlet");??
  50. ??try?{??
  51. ??file.upload(request);??
  52. ??}?catch?(error:Error)?{??
  53. ?????trace("上传失败");??
  54. ??}??
  55. ????
  56. ??}??
  57. ??}??
  58. ??]]>??
  59. ?</fx:Script>??
  60. ???
  61. ?<mx:Canvas?width="100%"?height="100%">??
  62. ??<mx:VBox?width="100%"?horizontalAlign="center">??
  63. ???<mx:Label?id="lbProgress"?text="上传"/>??
  64. ???<mx:ProgressBar?id="bar"?labelPlacement="bottom"?themeColor="#F20D7A"??
  65. ???????minimum="0"?visible="true"?maximum="100"?label="当前进度:?0%"???
  66. ???????direction="right"?mode="manual"?width="200"/>??
  67. ???<mx:Button?label="上传文件"?click="upload();"/>?????????????
  68. ??</mx:VBox>??
  69. ?</mx:Canvas>??
  70. </s:Application>??

2. java Servlet端代码:

?

[java] view plain copy print ?
  1. package?test;??
  2. ??
  3. import?java.io.File;??
  4. import?java.io.IOException;??
  5. import?java.io.PrintWriter;??
  6. import?java.util.Iterator;??
  7. import?java.util.List;??
  8. ??
  9. import?javax.servlet.ServletException;??
  10. import?javax.servlet.http.HttpServlet;??
  11. import?javax.servlet.http.HttpServletRequest;??
  12. import?javax.servlet.http.HttpServletResponse;??
  13. ??
  14. import?org.apache.commons.fileupload.FileItem;??
  15. import?org.apache.commons.fileupload.FileUploadException;??
  16. import?org.apache.commons.fileupload.disk.DiskFileItemFactory;??
  17. import?org.apache.commons.fileupload.servlet.ServletFileUpload;??
  18. ??
  19. public?class?FileUploadServlet?extends?HttpServlet?{??
  20. ??
  21. ?private?static?final?long?serialVersionUID?=?1L;??
  22. //存放文件路径 ??
  23. ?private?String?uploadPath?=?"D:/Tomcat?6.0/webapps/firstFlex/bin-debug/";??
  24. ??
  25. ??
  26. ?private?int?maxPostSize?=?100?*?1024?*?1024;??
  27. ??
  28. ?public?FileUploadServlet()?{??
  29. ??super();??
  30. ?}??
  31. ??
  32. ?public?void?destroy()?{??
  33. ??super.destroy();??
  34. ?}??
  35. ??
  36. ??public?void?doGet(HttpServletRequest?request,?HttpServletResponse?response)??
  37. ???throws?ServletException,?IOException?{??
  38. ???
  39. ??
  40. //??String?dressId?=?request.getParameter("dressID"); ??
  41. //?????System.out.println(dressId); ??
  42. ???
  43. ??//防止标题中还有中文字符是出现乱码 ??
  44. ??request.setCharacterEncoding("UTF-8");??
  45. ??response.setContentType("text/html;?charset=UTF-8");??
  46. ??DiskFileItemFactory?factory?=?new?DiskFileItemFactory();??
  47. ??factory.setSizeThreshold(4096);??
  48. ??ServletFileUpload?upload?=?new?ServletFileUpload(factory);??
  49. ??upload.setSizeMax(maxPostSize);??
  50. ??try??
  51. ??{??
  52. ???List?fileItems?=?upload.parseRequest(request);??
  53. ???Iterator?iter?=?fileItems.iterator();??
  54. ???while?(iter.hasNext())??
  55. ???{??
  56. ????FileItem?item?=?(FileItem)?iter.next();??
  57. ????if?(!item.isFormField())??
  58. ????{??
  59. ?????String?name?=?item.getName();??
  60. ?????System.out.println(name);??
  61. ?????try??
  62. ?????{??
  63. ??????item.write(new?File(uploadPath?+?name));??
  64. ?????}??
  65. ?????catch?(Exception?e)??
  66. ?????{??
  67. ??????e.printStackTrace();??
  68. ?????}??
  69. ????}??
  70. ???}??
  71. ??}??
  72. ??catch?(FileUploadException?e)??
  73. ??{??
  74. ???e.printStackTrace();??
  75. ??}??
  76. ?}??
  77. ??
  78. ??public?void?doPost(HttpServletRequest?request,?IOException?{??
  79. ??doGet(request,?response);??
  80. ?}??
  81. ??
  82. ?public?void?init()?throws?ServletException?{??
  83. ??//?Put?your?code?here ??
  84. ?}??
  85. ??
  86. }??
  87. ??
  88. 3.下面是web.xml文件Servlet的配置部分,把它加入到web.xml中即可。??
  89. ??
  90. ??<servlet>????
  91. ????????<servlet-name>fileUploadServlet</servlet-name>????
  92. ????????<servlet-class>test.FileUploadServlet</servlet-class>????
  93. ????</servlet>????????
  94. ????<servlet-mapping>????
  95. ????????<servlet-name>fileUploadServlet</servlet-name>????
  96. ????????<url-pattern>/fileUploadServlet</url-pattern>????
  97. ????</servlet-mapping>????

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读