springMVC兩種方式實現多文件上傳及效率比較

springMVC實現多文件上傳的方式有兩種,一種是我們經常使用的以字節流的方式進行文件上傳,另外一種是使用springMVC包裝好的解析器進行上傳。這兩種方式對于實現多文件上傳效率上卻有著很大的差距,下面我們通過實例來看一下這兩種方式的實現方式,同時比較一下在效率上到底存在著多大的差距。

1.下載相關jar包。需要引入的jar出了springMVC的jar包外,還需要引入com.springsource.org.apache.commons.fileupload-1.2.0.jar和com.springsource.org.apache.commons.io-1.4.0.jar。所有的jar包可以通過“點擊這里”進行下載。

2.配置springAnnotation-servlet.xml文件(文件名稱可以自定義,只要和web.xml中引入的名稱一樣即可):

[html]?view plaincopy
  1. <?xml?version="1.0"?encoding="UTF-8"?>??
  2. <!--?Bean頭部?-->??
  3. <beans?xmlns="http://www.springframework.org/schema/beans"??
  4. ????xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"??
  5. ????xmlns:p="http://www.springframework.org/schema/p"??
  6. ????xmlns:mvc="http://www.springframework.org/schema/mvc"??
  7. ????xmlns:context="http://www.springframework.org/schema/context"??
  8. ????xmlns:util="http://www.springframework.org/schema/util"??
  9. ????xsi:schemaLocation="http://www.springframework.org/schema/beans?http://www.springframework.org/schema/beans/spring-beans-3.0.xsd??
  10. ????????????http://www.springframework.org/schema/context?http://www.springframework.org/schema/context/spring-context-3.0.xsd??
  11. ????????????http://www.springframework.org/schema/mvc?http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd??
  12. ????????????http://www.springframework.org/schema/util?http://www.springframework.org/schema/util/spring-util-3.0.xsd">??
  13. ?????
  14. ???<!--?注解掃描包?-->??
  15. ???<context:component-scan?base-package="com.tgb.web.controller.annotation"></context:component-scan>??
  16. ???<!--?代替下面的兩行代碼?-->??
  17. ???<mvc:annotation-driven/>??
  18. ????
  19. ???<!--?靜態資源訪問?-->??
  20. ???<mvc:resources?location="/img/"?mapping="/img/**"/>??
  21. ????<mvc:resources?location="/js/"?mapping="/js/**"/>??
  22. ?????
  23. ?????
  24. ???<bean?id="viewResolver"?class="org.springframework.web.servlet.view.InternalResourceViewResolver">??
  25. ????????<property?name="prefix"?value="/"></property>??
  26. ????????<property?name="suffix"?value=".jsp"></property>??
  27. ??????
  28. ???</bean>??
  29. ?????
  30. ???<bean?id="multipartResolver"?class="org.springframework.web.multipart.commons.CommonsMultipartResolver">??
  31. ????????<property?name="defaultEncoding"?value="utf-8"></property>???
  32. ????????<property?name="maxUploadSize"?value="10485760000"></property>??
  33. ????????<property?name="maxInMemorySize"?value="40960"></property>??
  34. ???</bean>??
  35. </beans>??
3.?配置web.xml文件:

[html]?view plaincopy
  1. <?xml?version="1.0"?encoding="UTF-8"?>??
  2. <web-app?xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"?xmlns="http://java.sun.com/xml/ns/javaee"?xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"?xsi:schemaLocation="http://java.sun.com/xml/ns/javaee?http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"?id="WebApp_ID"?version="2.5">??
  3. ??<display-name>springMVC1</display-name>??
  4. ??<welcome-file-list>??
  5. ????<welcome-file>index.html</welcome-file>??
  6. ??</welcome-file-list>??
  7. ????
  8. ??<servlet>??
  9. ?????<servlet-name>springMVC</servlet-name>??
  10. ?????<!--?springMVC的分發器?-->??
  11. ?????<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>??
  12. ?????<init-param>??
  13. ????????<param-name>contextConfigLocation</param-name>??
  14. ????????<param-value>classpath*:config/springAnnotation-servlet.xml</param-value>??
  15. ?????</init-param>??
  16. ?????<!--?表示當Tomcat已啟動的時候初始化這個Servlet?-->??
  17. ?????<load-on-startup>1</load-on-startup>??
  18. ??</servlet>??
  19. ????
  20. ??<filter>????
  21. ??????<filter-name>encodingFilter</filter-name>????
  22. ??????<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>????
  23. ??????<init-param>????
  24. ??????????<param-name>encoding</param-name>????
  25. ??????????<param-value>UTF-8</param-value>?<!--設置你想用的字符集,我這里用的是GB18030-->????
  26. ??????</init-param>????
  27. ??????<init-param>??
  28. ????????<param-name>forceEncoding</param-name>??
  29. ????????<param-value>true</param-value>??
  30. ??????</init-param>??
  31. ??</filter>????
  32. ??????
  33. ??<filter-mapping>????
  34. ??????<filter-name>encodingFilter</filter-name>????
  35. ??????<url-pattern>/*</url-pattern>?<!--設置你想過濾的頁面或者是Servlet,根據自己的需要配置-->????
  36. ?</filter-mapping>????
  37. ???
  38. ??<servlet-mapping>??
  39. ????<servlet-name>springMVC</servlet-name>??
  40. ????<url-pattern>/</url-pattern>??
  41. ??</servlet-mapping>??
  42. </web-app>??
4.?jsp頁面代碼:
[html]?view plaincopy
  1. <%@?page?language="java"?contentType="text/html;?charset=UTF-8"??
  2. ????pageEncoding="UTF-8"%>??
  3. <!DOCTYPE?html?PUBLIC?"-//W3C//DTD?HTML?4.01?Transitional//EN"?"http://www.w3.org/TR/html4/loose.dtd">??
  4. <html>??
  5. <head>??
  6. <script?type="text/javascript"?src="../js/jquery-1.7.2.js"></script>??
  7. <meta?http-equiv="Content-Type"?content="text/html;?charset=UTF-8">??
  8. <title>Insert?title?here</title>??
  9. <script?type="text/javascript">??
  10. ????i?=?1;??
  11. ????j?=?1;??
  12. ????$(document).ready(function(){??
  13. ??????????
  14. ????????$("#btn_add1").click(function(){??
  15. ????????????document.getElementById("newUpload1").innerHTML+='<div?id="div_'+i+'"><input??name="file"?type="file"??/><input?type="button"?value="刪除"??onclick="del_1('+i+')"/></div>';??
  16. ??????????????i?=?i?+?1;??
  17. ????????});??
  18. ??????????
  19. ????????$("#btn_add2").click(function(){??
  20. ????????????document.getElementById("newUpload2").innerHTML+='<div?id="div_'+j+'"><input??name="file_'+j+'"?type="file"??/><input?type="button"?value="刪除"??onclick="del_2('+j+')"/></div>';??
  21. ??????????????j?=?j?+?1;??
  22. ????????});??
  23. ????});??
  24. ??
  25. ????function?del_1(o){??
  26. ?????document.getElementById("newUpload1").removeChild(document.getElementById("div_"+o));??
  27. ????}??
  28. ??????
  29. ????function?del_2(o){??
  30. ?????????document.getElementById("newUpload2").removeChild(document.getElementById("div_"+o));??
  31. ????}??
  32. ??
  33. </script>??
  34. </head>??
  35. <body>??
  36. ??
  37. ?????<h1>springMVC字節流輸入上傳文件</h1>???
  38. ????<form?name="userForm1"?action="/springMVC7/file/upload"?enctype="multipart/form-data"?method="post">??
  39. ????????<div?id="newUpload1">??
  40. ????????????<input?type="file"?name="file">??
  41. ????????</div>??
  42. ??????????
  43. ????????<input?type="button"?id="btn_add1"?value="增加一行"?>??
  44. ????????<input?type="submit"?value="上傳"?>??
  45. ????</form>???
  46. ????<br>??
  47. ????<br>??
  48. ????<hr?align="left"?width="60%"?color="#FF0000"?size="3">??
  49. ????<br>??
  50. ????<br>??
  51. ?????<h1>springMVC包裝類上傳文件</h1>???
  52. ????<form?name="userForm2"?action="/springMVC7/file/upload2"?enctype="multipart/form-data"?method="post"">??
  53. ????????<div?id="newUpload2">??
  54. ????????????<input?type="file"?name="file">??
  55. ????????</div>??
  56. ????????<input?type="button"?id="btn_add2"?value="增加一行"?>??
  57. ????????<input?type="submit"?value="上傳"?>??
  58. ??????????
  59. ??????????
  60. ????</form>???
  61. </body>??
  62. </html>??
5.實現上傳功能的java bean:
[java]?view plaincopy
  1. package?com.tgb.web.controller.annotation.upload;??
  2. ??
  3. import?java.io.File;??
  4. import?java.io.FileInputStream;??
  5. import?java.io.FileOutputStream;??
  6. import?java.io.IOException;??
  7. import?java.io.PrintWriter;??
  8. import?java.io.UnsupportedEncodingException;??
  9. import?java.net.URLDecoder;??
  10. import?java.util.Date;??
  11. import?java.util.Iterator;??
  12. ??
  13. import?javax.servlet.http.HttpServletRequest;??
  14. import?javax.servlet.http.HttpServletResponse;??
  15. import?javax.swing.filechooser.FileNameExtensionFilter;??
  16. ??
  17. import?org.springframework.stereotype.Controller;??
  18. import?org.springframework.web.bind.annotation.RequestMapping;??
  19. import?org.springframework.web.bind.annotation.RequestMethod;??
  20. import?org.springframework.web.bind.annotation.RequestParam;??
  21. import?org.springframework.web.multipart.MultipartFile;??
  22. import?org.springframework.web.multipart.MultipartHttpServletRequest;??
  23. import?org.springframework.web.multipart.commons.CommonsMultipartFile;??
  24. import?org.springframework.web.multipart.commons.CommonsMultipartResolver;??
  25. import?org.springframework.web.servlet.ModelAndView;??
  26. ??
  27. ??
  28. import?com.tgb.web.controller.entity.User;??
  29. ??
  30. @Controller??
  31. @RequestMapping("/file")??
  32. public?class?UploadController?{???
  33. ??????
  34. ??????
  35. ????@RequestMapping("/upload"???)??
  36. ????public?String?addUser(@RequestParam("file")?CommonsMultipartFile[]?files,HttpServletRequest?request){??
  37. ??????????
  38. ????????for(int?i?=?0;i<files.length;i++){??
  39. ????????????System.out.println("fileName---------->"?+?files[i].getOriginalFilename());??
  40. ??????????
  41. ????????????if(!files[i].isEmpty()){??
  42. ????????????????int?pre?=?(int)?System.currentTimeMillis();??
  43. ????????????????try?{??
  44. ????????????????????//拿到輸出流,同時重命名上傳的文件??
  45. ????????????????????FileOutputStream?os?=?new?FileOutputStream("H:/"?+?new?Date().getTime()?+?files[i].getOriginalFilename());??
  46. ????????????????????//拿到上傳文件的輸入流??
  47. ????????????????????FileInputStream?in?=?(FileInputStream)?files[i].getInputStream();??
  48. ??????????????????????
  49. ????????????????????//以寫字節的方式寫文件??
  50. ????????????????????int?b?=?0;??
  51. ????????????????????while((b=in.read())?!=?-1){??
  52. ????????????????????????os.write(b);??
  53. ????????????????????}??
  54. ????????????????????os.flush();??
  55. ????????????????????os.close();??
  56. ????????????????????in.close();??
  57. ????????????????????int?finaltime?=?(int)?System.currentTimeMillis();??
  58. ????????????????????System.out.println(finaltime?-?pre);??
  59. ??????????????????????
  60. ????????????????}?catch?(Exception?e)?{??
  61. ????????????????????e.printStackTrace();??
  62. ????????????????????System.out.println("上傳出錯");??
  63. ????????????????}??
  64. ????????}??
  65. ????????}??
  66. ????????return?"/success";??
  67. ????}??
  68. ??????
  69. ??????
  70. ????@RequestMapping("/upload2"??)??
  71. ????public?String?upload2(HttpServletRequest?request,HttpServletResponse?response)?throws?IllegalStateException,?IOException?{??
  72. ????????//創建一個通用的多部分解析器??
  73. ????????CommonsMultipartResolver?multipartResolver?=?new?CommonsMultipartResolver(request.getSession().getServletContext());??
  74. ????????//判斷?request?是否有文件上傳,即多部分請求??
  75. ????????if(multipartResolver.isMultipart(request)){??
  76. ????????????//轉換成多部分request????
  77. ????????????MultipartHttpServletRequest?multiRequest?=?(MultipartHttpServletRequest)request;??
  78. ????????????//取得request中的所有文件名??
  79. ????????????Iterator<String>?iter?=?multiRequest.getFileNames();??
  80. ????????????while(iter.hasNext()){??
  81. ????????????????//記錄上傳過程起始時的時間,用來計算上傳時間??
  82. ????????????????int?pre?=?(int)?System.currentTimeMillis();??
  83. ????????????????//取得上傳文件??
  84. ????????????????MultipartFile?file?=?multiRequest.getFile(iter.next());??
  85. ????????????????if(file?!=?null){??
  86. ????????????????????//取得當前上傳文件的文件名稱??
  87. ????????????????????String?myFileName?=?file.getOriginalFilename();??
  88. ????????????????????//如果名稱不為“”,說明該文件存在,否則說明該文件不存在??
  89. ????????????????????if(myFileName.trim()?!=""){??
  90. ????????????????????????System.out.println(myFileName);??
  91. ????????????????????????//重命名上傳后的文件名??
  92. ????????????????????????String?fileName?=?"demoUpload"?+?file.getOriginalFilename();??
  93. ????????????????????????//定義上傳路徑??
  94. ????????????????????????String?path?=?"H:/"?+?fileName;??
  95. ????????????????????????File?localFile?=?new?File(path);??
  96. ????????????????????????file.transferTo(localFile);??
  97. ????????????????????}??
  98. ????????????????}??
  99. ????????????????//記錄上傳該文件后的時間??
  100. ????????????????int?finaltime?=?(int)?System.currentTimeMillis();??
  101. ????????????????System.out.println(finaltime?-?pre);??
  102. ????????????}??
  103. ??????????????
  104. ????????}??
  105. ????????return?"/success";??
  106. ????}??
  107. ??????
  108. ????@RequestMapping("/toUpload"?)???
  109. ????public?String?toUpload()?{??
  110. ??????????
  111. ????????return?"/upload";??
  112. ????}??
  113. ??????
  114. }??

6.最后看后臺打印數據,數據來源于后臺打印的上傳文件所用的時間,第一幅圖片是使用字節流寫入方式完成三個文件上傳中每個文件用時,第二幅圖片是使用springMVC包裝好的解析器進行的三個相同的文件上傳中每個文件的用時:

字節流實現文件上傳的傳遞效率,結果顯示傳遞三個文件用時分別為534ms,453ms和387ms。


使用springMVC解析器進行文件上傳用時分別為2ms,1ms和2ms。


通過對比這兩種方式我們可以發現使用springMVC進行多文件的效率顯然要比字符流寫入方式效率上要高得多。


本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/539398.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/539398.shtml
英文地址,請注明出處:http://en.pswp.cn/news/539398.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

c語言 個位,如何才能給C語言增加幾個位操作函數

在匯編語言中有直接對位進行操作的指令&#xff0c;如置位、復位、位取反、測試某一位等&#xff0c;這對于硬件操作十分方便&#xff0c;在C語言中盡管也提供了一些位操作手段&#xff0c;如按位與、按位或、按位取反等&#xff0c;但它們是對一個字節進行操作&#xff0c;如要…

hsrp 切換_HSRP、VRRP、GLBP | 網絡工程師之網關高可用、冗余

在RS的學習過程中我們接觸到很多網絡技術&#xff0c;后面就把工作中常用的拿來與大家分享&#xff0c;本次我們來分享網關冗余技術。當我們的網關設備無法使用堆疊(VSS,istack&#xff0c;IRF)&#xff0c;或者不同廠商設備的時候&#xff0c;非常有效&#xff0c;能夠提供網關…

Linux文件系統詳解

從操作系統的角度詳解Linux文件系統層次、文件系統分類、文件系統的存儲結構、不同存儲介質的區別(RAM、ROM、Flash)、存儲節點inode。本文參考&#xff1a; http://blog.chinaunix.net/uid-8698570-id-1763151.htmlhttp://www.iteye.com/topic/816268http://soft.chinabyte.co…

opencv機器學習線性回歸_機器學習(線性回歸(二))

Lasso與嶺回歸的同和異Lasso、嶺回歸都可以預防模型過擬合Lasso回歸懲罰項為L1正則&#xff0c;嶺回歸為L2正則Lasso回歸可用來特征選擇&#xff0c;嶺回歸則不能Lasso回歸用坐標下降法求解&#xff0c;嶺回歸用梯度下降法求解。為什么Lasso可用于特征選擇&#xff0c;而嶺回歸…

c語言int a什么意思,問一下吧里大神 int a = a; 這么定義是什么意思?

該樓層疑似違規已被系統折疊 隱藏此樓查看此樓使用自身初始化&#xff0c;具有indeterminate value&#xff0c;可能是trap representation導致使用這個對象的值引起undefined behavior。但這個初始化語法上是正確的&#xff0c;也沒有語義錯誤。ISO C113.19.21 indeterminate …

把EXCEL用程序導入到ORACLE中(SpringMVC+MyBatis)

前提&#xff1a;項目中需要把EXCEL數據批量導入oracle中兩張表中。如是用到了poi技術。分別導入poi-3.11-beta2.jar和poi-ooxml-schemas-3.9.jar這兩個包。EXCEL數據如下 第一步&#xff1a;修改spring框架配置文件。 springmvc-servlet.xml加上&#xff1a; <!-- 文件上傳…

access實例_西門子PLC1200組態王跟Access數據庫-⑥組態王變量

西門子PLC1200&#xff0c;組態王跟Access數據庫--⑥組態王變量組態王的變量設置1.0 變量設置如下圖所示&#xff0c;選擇數據詞典&#xff0c;里面是系統變量跟新建的變量&#xff0c;選擇新建1.1 如下圖&#xff0c;從上往下依次&#xff0c;變量名(這個應該知道)&#xff0c…

Android 擼起袖子,自己封裝 DialogFragment

前言 具體的代碼以及示例我都放上 Github 了&#xff0c;有需要的朋友可以去看一下 DialogFragmentDemos&#xff0c;歡迎 star 和 fork. 本文的主要內容 DialogFragment 是什么創建通用的 CommonDialogFragment實現各種類型的 DialogFragment在寫正文之前&#xff0c;先來一波…

as模擬器文件夾路徑_EGG Switch手機模擬器怎么用?中文教程來了......

大家好&#xff0c;小雞妹我又來啦。自從昨天曝光了美國NX工作室的EGG模擬器之后&#xff0c;推文底下就多了一千多條留言&#xff0c;說啥的都有。不過小雞妹大概總結了一下&#xff0c;發現下面這幾條問題&#xff0c;出現的頻率最高&#xff1a;① 找不到網址&#xff1b;②…

xml文件c語言讀取函數,讀寫xml文件的2個小函數

#region 讀寫xml文件的2個小函數&#xff0c;2005 4 2 by hycpublic void SetXmlFileValue(string xmlPath,string AppKey,string AppValue)//寫xmlPath是文件路徑文件名&#xff0c;AppKey是 Key Name&#xff0c;AppValue是Value{XmlDocument xDoc new XmlDocument();xDoc.L…

Java導入導出Excel工具類ExcelUtil

前段時間做的分布式集成平臺項目中&#xff0c;許多模塊都用到了導入導出Excel的功能&#xff0c;于是決定封裝一個ExcelUtil類&#xff0c;專門用來處理Excel的導入和導出 本項目的持久化層用的是JPA&#xff08;底層用hibernate實現&#xff09;&#xff0c;所以導入和導出也…

郁金香匯編代碼注入怎么寫看雪_世界黑客編程大賽冠軍的匯編代碼 你見過嗎?...

前幾天發布了一篇“雷軍22年前寫的匯編代碼”的文章&#xff0c;引起網友的熱議。有人說匯編是最牛逼的編程語言&#xff0c;沒有之一。匯編語言確實厲害&#xff0c;不知道你有沒有見過世界黑客編程大賽冠軍的作品?雷軍編寫的的匯編代碼有 網友分享了97年Mekka ’97 4K Intro…

變位齒輪重合度計算公式_齒輪“模數”是如何計算的?

模數是決定齒輪大小的因素。齒輪模數被定義為模數制輪齒的一個基本參數&#xff0c;是人為抽象出來用以度量輪齒規模的數。目的是標準化齒輪刀具&#xff0c;減少成本。直齒、斜齒和圓錐齒齒輪的模數皆可參考標準模數系列表。工業定義&#xff1a;齒輪的分度圓是設計、計算齒輪…

c語言改錯和填空能運行嗎,C語言改錯填空編程

改錯題1、在考生文件夾下&#xff0c;給定程序MODI.C的功能是&#xff1a;從低位開始取出長整型變量s中奇數位上的數&#xff0c;依次構成一個新數放在t中。例如&#xff0c;當s中的數為&#xff1a;7654321時&#xff0c;t中的數為&#xff1a;7531。請修改并運行該程序&#…

@Autowired注解實現原理

在討論代碼細節之前&#xff0c;我們再來了解下基礎知識。Spring管理可用于整個應用程序的Java對象bean。他們所在的Spring容器&#xff0c;被稱為應用程序上下文。這意味著我們不需要處理他們的生命周期(初始化&#xff0c;銷毀)。該任務由此容器來完成。另外&#xff0c;該上…

獲取freemarker處理后的內容

相信很多人都用過freemarker&#xff0c;或做視圖&#xff0c;或模板&#xff0c;或生成靜態文件等,但是有多少人做過這樣的應用&#xff0c;通過模板后&#xff0c;不是要輸出靜態的內容&#xff0c;而是直接在代碼中獲取處理模板后的內容&#xff0c;研究了下API,freemarker里…

c4.5算法python實現_算法:用Python實現—最優化算法

今天給大家分享一下算法&#xff0c;用python來實現最優化算法。廢話不多說&#xff0c;直接上代碼&#xff1a;一、二分法函數詳見rres&#xff0c;此代碼使該算法運行了兩次def asdf(x): rres8*x**3-2*x**2-7*x3 return rresi2left0right1while i>0 : i i-1 …

comsol臨時文件夾中有不支持的字符_文件名中不能包含的字符

文件名是為了方便人們區分計算機中的不同文件&#xff0c;而給每個文件設定一個指定的名稱。由文件主名和擴展名組成。DOS操作系統規定文件名由文件主名和擴展名組成&#xff0c;文件主名由1~8個字符組成&#xff0c;擴展名由1~3個字符組成&#xff0c;主名和擴展名之間由一個小…

linux 星號 通配符,如何在bash中轉義通配符/星號字符?

簡短的回答像其他人所說的那樣 - 你應該總是引用變量來防止奇怪的行為。所以使用echo“$ foo”代替echo $ foo。長期回答我確實認為這個例子值得進一步解釋&#xff0c;因為它的表面看起來比它看起來更多。我可以看到你的困惑在哪里&#xff0c;因為在你運行你的第一個例子后&a…

PYTHON面試

大部分的面試問題&#xff0c;有最近要找事的老鐵嗎&#xff1f;python語法以及其他基礎部分可變與不可變類型&#xff1b; 淺拷貝與深拷貝的實現方式、區別&#xff1b;deepcopy如果你來設計&#xff0c;如何實現&#xff1b; __new__() 與 __init__()的區別&#xff1b; 你知…