มีสองวิธีในการใช้การอัปโหลดแบบหลายไฟล์ใน SpringMVC หนึ่งคือการอัปโหลดไฟล์ในสตรีมไบต์ที่เรามักจะใช้และอื่น ๆ คือการอัปโหลดโดยใช้ตัวแยกวิเคราะห์ที่ห่อหุ้มใน SpringMVC สองวิธีนี้มีช่องว่างขนาดใหญ่ในประสิทธิภาพของการอัพโหลดหลายไฟล์ ลองมาดูวิธีการใช้งานของวิธีการทั้งสองนี้ผ่านตัวอย่างและเปรียบเทียบว่าช่องว่างของประสิทธิภาพมีขนาดใหญ่แค่ไหน
1. ดาวน์โหลดแพ็คเกจ Jar ที่เกี่ยวข้อง นอกเหนือจากแพ็คเกจ JAR ของ SpringMVC, com.springsource.org.apache.commons.fileupload-1.2.0.jar และ com.springsource.org.apache.Commons.io-1.4.0.jar และ com.springsource
2. กำหนดค่าไฟล์ Springannotation-servlet.xml (ชื่อไฟล์สามารถปรับแต่งได้ตราบใดที่มันเหมือนกับชื่อที่แนะนำใน web.xml):
<? XML เวอร์ชัน = "1.0" การเข้ารหัส = "UTF-8"?> <!-Bean Header-> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://ww. xmlns: p = "http://www.springframework.org/schema/p" xmlns: mvc = "http://www.springframework.org/schema/mvc" xmlns: บริบท = xmlns: util = "http://www.springframework.org/schema/util" xsi: schemalocation = "http://www.springframework.org/schema/beans http://www.springframeworkwork http://www.springframework.org/schema/context http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.org http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd "> <! base-package = "com.tgb.web.controller.annotation"> </บริบท: Component-scan> <!-แทนที่รหัสสองบรรทัดต่อไปนี้-> <mvc: การเพิ่มความคิดเห็น/> <! mapping = "/js/**"/> <bean id = "viewResolver"> <property name = "คำนำหน้า" value = "/"> </property> <property name = "คำต่อท้าย" value = ". jsp"> </property> </epean> name = "MaxUploadSize" value = "10485760000"> </คุณสมบัติ> <property name = "MaxInMemorySize" value = "40960"> </property> </ebean> </ebeans>
3. กำหนดค่าไฟล์ web.xml:
<? xml version = "1.0" การเข้ารหัส = "utf-8"?> <web-app xmlns: xsi = "http://www.w3.org/2001/xmlschema-instance" xmlns = "http://java.sun.com/xml xmlns: web = "http://java.sun.com/xml/ns/javaee" xmlns: web = "http://java.sun.com/xml/ns/javaee/web-app_2_5_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" เวอร์ชัน = "2.5" <welcome-file> index.html </welcome-file> </welcome-file-list> <servlet> <servlet-name> springmvc </servlet-name> <!-ผู้จัดจำหน่าย SpringMvc-> <servlet-class> org.springframework.web.servlet.dispersert <param-value> classpath*: config/springannotation-servlet.xml </param-value> </init-param> <!-ระบุว่า servlet เริ่มต้นเมื่อ Tomcat เริ่มต้น-> <load-on-startup> 1 </load-on-startup> <Tilter-class> org.springframework.web.filter.characterencodingFilter </filter-class> <init-Param> <param-name> การเข้ารหัส </param-name> ฉันใช้ GB18030-> </init-param> <init-Param> <param-Name> ForceEncoding </param-name> <param-value> True </param-value> </init-param> </filter> <filter-mapping> คุณต้องการกรองกำหนดค่าตามความต้องการของคุณ-> </filter-mapping> <servlet-mapping> <servlet-name> SpringMvc </servlet-name> <url-pattern>/</url-pattern>
4. รหัสหน้า JSP:
<%@ page language = "java" contentType = "ข้อความ/html; charset = utf-8" pageencoding = "utf-8"%> <! doctype html สาธารณะ "-// w3c // dtd html 4.01 transitional // en" <html> <head> <script type = "text/javaScript" src = "../ js/jquery-1.7.2.js"> </script> <meta http-equiv = "content-type" type/"text/html; charset = utf-8"> <title> J = 1; $ (document) .ready (function () {$ ("#btn_add1") คลิก (function () {document.getElementById ("newupload1"). innerhtml+= '<div id = "div _'+i+'"> <อินพุตชื่อ = "ไฟล์" onclick = "del_1 ('+i+')"/> </div> '; /> <อินพุต Type = "button" value = "delete" onclick = "del_2 ('+j+')"/> </div> ฟังก์ชั่น del_1 (o) {document.getElementById ("newupload1"). RemoveChild (document.getElementById ("Div _"+O)); } ฟังก์ชั่น del_2 (o) {document.getElementById ("newupload2"). RemoveChild (document.getElementById ("div _"+o)); } </script> </head> <body> <h1> SpringMvc BYTE สตรีมอินพุตไฟล์อัปโหลด </h1> <form name = "userform1" action = "/springmvc7/file/upload" enctype = "multipart/form-data" file = "post"> <div id = "newUpload1" id = "btn_add1" value = "เพิ่มหนึ่งบรรทัด"> <อินพุตประเภท = "ส่ง" value = "อัปโหลด"> </form> <br> <hr align = "left" color = "#ff0000" size = "3"> <br> <h1> enctype = "multipart/form-data" method = "post" "> <div id =" newupload2 "> <อินพุต type =" file "name =" ไฟล์ "> </div> <อินพุตประเภท =" ปุ่ม "id =" btn_add2 "value =" เพิ่มหนึ่งบรรทัด "> <5. Java Bean ที่ใช้ฟังก์ชั่นการอัปโหลด:
แพ็คเกจ com.tgb.web.controller.annotation.upload; นำเข้า Java.io.File; นำเข้า Java.io.FileInputStream; นำเข้า Java.io.FileOutputStream; นำเข้า java.io.ioException; นำเข้า java.io.printwriter; นำเข้า java.io.unsupportencodingexception; นำเข้า java.net.urldecoder; นำเข้า java.util.date; นำเข้า java.util.iterator; นำเข้า javax.servlet.http.httpservletrequest; นำเข้า Javax.servlet.http.httpservletResponse; นำเข้า javax.swing.filechooser.filenameextensionFilter; นำเข้า org.springframework.stereotype.controller; นำเข้า org.springframework.web.bind.annotation.requestmapping; นำเข้า org.springframework.web.bind.annotation.requestmethod; นำเข้า org.springframework.web.bind.annotation.requestparam; นำเข้า org.springframework.web.multipart.multipartfile; นำเข้า org.springframework.web.multipart.multiparthttpservletrequest; นำเข้า org.springframework.web.multipart.Commons.CommonsMultipartFile; นำเข้า org.springframework.web.multipart.Commons.CommonsMultipartResolver; นำเข้า org.springframework.web.servlet.modelandview; นำเข้า com.tgb.web.controller.entity.user; @Controller @RequestMapping ("/ไฟล์") คลาสสาธารณะ UploadController {@requestmapping ("/อัปโหลด") สตริงสาธารณะ adduser (@requestparam ("ไฟล์") ไฟล์ commonsmultipartfile [] {{for for {สำหรับ (int i = 0; System.out.println ("ชื่อไฟล์ ----------->" + ไฟล์ [i] .getoriginalfilename ()); if (! files [i] .isEmpty ()) {int pre = (int) system.currentTimeMillis (); ลอง {// รับกระแสเอาต์พุตและเปลี่ยนชื่อไฟล์ไฟล์ที่อัปโหลด FileOutputStream OS = ใหม่ fileOutputStream ("H:/" + วันที่ใหม่ () getTime () + ไฟล์ [i] .getOriginalFileName ()); // รับสตรีมอินพุตของไฟล์ไฟล์ที่อัปโหลด FileInputStream ใน = (fileInputStream) ไฟล์ [i] .getInputStream (); // เขียนไฟล์ int b = 0; ในขณะที่ ((b = in.read ())! = -1) {os.write (b); } os.flush (); os.close (); in.close (); int สุดท้าย = (int) system.currenttimeMillis (); System.out.println (FinalTime - Pre); } catch (exception e) {e.printstacktrace (); System.out.println ("UploaderRor"); }}} return "/success"; } @requestmapping ("/upload2") สตริงสาธารณะ upload2 (คำขอ httpservletrequest, httpservletResponse การตอบสนอง) พ่น unledlegalStateException, ioException {// สร้างตัวแก้ไขหลาย ๆ // ปกป้องว่าคำขอมีการอัปโหลดไฟล์นั่นคือถ้า (MultiPartResolver.ismultipart (คำขอ)) {// แปลงเป็น MultipARTENT Request MultiparthttpServletRequest Multirequest = (MultiparthttpServletRequest) // รับชื่อไฟล์ทั้งหมดในคำขอ iterator <string> iter = multirequest.getFilenames (); ในขณะที่ (iter.hasnext ()) {// บันทึกเวลาที่จุดเริ่มต้นของกระบวนการอัปโหลดและใช้เพื่อคำนวณเวลาอัปโหลด int pre = (int) system.currentTimeMillis (); // รับไฟล์อัปโหลดไฟล์ multipartFile = multirequest.getFile (iter.next ()); if (file! = null) {// รับชื่อไฟล์ของสตริงไฟล์ที่อัปโหลดในปัจจุบัน myFilename = file.getoriginalFilename (); // ถ้าชื่อไม่ใช่ "" หมายความว่าไฟล์มีอยู่มิฉะนั้นหมายความว่าไฟล์ไม่มีอยู่ถ้า (myfilename.trim ())! = "") {system.out.println (myfilename); // เปลี่ยนชื่อสตริงชื่อไฟล์ที่อัปโหลดชื่อ filename = "demoupload" + file.getoriginalfilename (); // กำหนดเส้นทางสตริงของเส้นทางการอัปโหลด = "H:/" + ชื่อไฟล์; ไฟล์ localfile = ไฟล์ใหม่ (พา ธ ); file.transferto (localfile); }} // บันทึกเวลาหลังจากอัปโหลดไฟล์ int finalTime = (int) system.currentTimeMillis (); System.out.println (FinalTime - Pre); }} return "/success"; } @requestmapping ("/toupload") สตริงสาธารณะ toupload () {return "/upload"; - 6. ในที่สุดดูที่ข้อมูลการพิมพ์พื้นหลัง ข้อมูลมาจากเวลาที่ใช้ในการอัปโหลดไฟล์ที่พิมพ์ในพื้นหลัง ภาพแรกคือเวลาที่ใช้ในการอัปโหลดแต่ละไฟล์ในสามไฟล์โดยใช้การเขียนสตรีมไบต์ ภาพที่สองคือเวลาที่ใช้ในการอัปโหลดแต่ละไฟล์ในสามไฟล์ที่เหมือนกันโดยใช้ตัวแยกวิเคราะห์ที่ห่อหุ้มด้วย SpringMVC:
BYTE Stream ตระหนักถึงประสิทธิภาพการจัดส่งของการอัปโหลดไฟล์และผลลัพธ์แสดงให้เห็นว่าเวลาที่ต้องใช้ในการส่งไฟล์ทั้งสามไฟล์คือ 534ms, 453ms และ 387ms ตามลำดับ
เวลาในการอัปโหลดไฟล์โดยใช้ SpringMVC Parser คือ 2ms, 1ms และ 2ms ตามลำดับ
โดยการเปรียบเทียบสองวิธีนี้เราสามารถค้นหาว่าการใช้ SpringMVC สำหรับหลาย ๆ ไฟล์นั้นมีประสิทธิภาพมากกว่าการเขียนอักขระ
ข้างต้นเป็นเนื้อหาทั้งหมดของบทความนี้ ฉันหวังว่ามันจะเป็นประโยชน์ต่อการเรียนรู้ของทุกคนและฉันหวังว่าทุกคนจะสนับสนุน wulin.com มากขึ้น