ฟังก์ชั่นการอัปโหลดและดาวน์โหลดไฟล์ถูกนำมาใช้ในโครงการล่าสุด ฉันคิดว่าฟังก์ชั่นนี้มีความสำคัญมากกว่าดังนั้นฉันจึงแยกมันออกเป็นพิเศษและลองด้วยตัวเอง
ต่อไปนี้เป็นขั้นตอนเฉพาะสำหรับการอัปโหลดและดาวน์โหลดไฟล์ในสภาพแวดล้อมการกำหนดค่า SpringMVC สำหรับการอ้างอิงของคุณ เนื้อหาเฉพาะมีดังนี้
1. การกำหนดค่าพื้นฐาน:
Maven นำเข้าแพ็คเกจและการกำหนดค่า pom.xml นอกเหนือจากการพึ่งพาพื้นฐานของ SpringMVC แล้วยังจำเป็นต้องนำเข้า Commons-io.jsr และ Commons-fileupload.jar ใช้เมื่ออัปโหลดไฟล์:
<Project xmlns = "http://maven.apache.org/pom/4.0.0" xmlns: xsi = "http://www.w3.org/2001/xmlschema-instance" xsi: schemalocation = "http:/MANCMMMMMMMMMMMMMMMMMMMMMMMMMMBM http://maven.apache.org/maven-v4_0_0.xsd "> <moderVersion> 4.0.0 </modelversion> <s/modelversion> Webapp </name> <url> http://maven.apache.org </url> <uffer> <buildName> fulloadtest </finalname> <plugins> <!- การกำหนดค่าต่อไปนี้สามารถมั่นใจได้ว่ารุ่น JRE จะไม่เปลี่ยนแปลงทุกครั้ง หากคุณไม่ได้ตั้งค่าสิ่งนี้ JRE จะเปลี่ยนกลับเป็น 1.5 ทุกครั้งที่การอัปเดตบังคับถูกบังคับ-> <plugin> <ratifactid> Maven-Compiler-Plugin </artifactid> <Sersion> 2.3.2 </เวอร์ชัน> <การกำหนดค่า> <SOCONT> 1.7 </Source> <bootclasspath> $ {java.home} /lib/rt.jar </bootclasspath> </pilerArguments> </การกำหนดค่า> </plugin> </build> <cendencies> <การพึ่งพา> <roupID> org.springframework </groupId> <ratifactid> Spring-Webmvc </artifactid> <sersion> 4.0.6.release </version> </derness> <Sersion> 2.2.3 </Servive> </derctency> <การพึ่งพา> <roupId> com.fasterxml.jackson.core </groupId> <ratifactid> Jackson-Core </artifactid> <version> 2.2.3 </เวอร์ชัน> </การพึ่งพา <ArtIfactId> Jackson-Databind </artifactid> <sersion> 2.2.3 </เวอร์ชัน> </การพึ่งพาอาศัย> <การพึ่งพา> <roupId> Commons-FileUpload </groupId> <ratifactId> Commons-FileUpload </artifactid> <version> 1.3.1 </เวอร์ชัน> <ArtIfactId> Commons-io </artifactId> <version> 2.4 </เวอร์ชัน> </การพึ่งพาอาศัย> </pendencies> </project>การกำหนดค่าพื้นฐานของ 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/web-app_2_5.xsd" xsi: schemalocation = "http://java.sun.com/xml/ns/javaeee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd "id =" webapp_id "เวอร์ชัน =" 2.5 "> <filter> <Tilter-class> org.springframework.web.filter.CharacterencodingFilter </filter-class> <init-Param> <Secutional> การเข้ารหัส Charset </คำอธิบาย> <param-Name> การเข้ารหัส </param-name> <sider-name> encodingFilter </filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <servlet> <servlet-name> springmvc </servlet-name> <servlet-class> org.springframework.web.servlet <param-name> contextconfiglocation </param-name> <param-value> classpath: spring.xml </param-value> </init-param> </servlet> <servlet-mapping> <welcome-file> index.html </welcome-file> <welcome-file> index.htm </welcome-file> <welcome-file> index.jsp </welcome-file> <welcome-file> index.jsp </welcome-file> <welcome-file> default.html </elest-file> </welcome-file-list> </eb-app>
Spring Basic Configuration Spring.xml:
<? xml version = "1.0" การเข้ารหัส = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://ww.w3.org/2001/xml xmlns: p = "http://www.springframework.org/schema/p" xmlns: บริบท = "http://www.springframework.org/schema/contex XSI: schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http:/www.springframework http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/schema <บริบท: Annotation-config /> <MVC: คำอธิบายประกอบที่ขับเคลื่อนด้วย /> <บริบท: Component-Scan base-package = "Controllers" /> <!-เพื่อหลีกเลี่ยงการดาวน์โหลดไฟล์จากการกลับไปยัง JSON เมื่อ IE ดำเนินการ Ajax-> <value> text/html; charset = utf-8 </value> </list> </property> </ebean> <!-เพื่อเปิดใช้งานฟังก์ชั่นคำอธิบายประกอบ MVC ฤดูใบไม้ผลิเพื่อให้การแมปของคำขอและคำอธิบายประกอบ POJOS-> <bean> Converter-> </list> </property> </ebean> <!-สนับสนุนการอัปโหลดไฟล์-> <bean id = "MultipartResolver"> <!-100m-> <property name = "MaxUploadSize" value = "104857600"> </คุณสมบัติ>
2. รหัสอัปโหลดไฟล์
สำหรับ index.html หน้าเว็บที่อัปโหลดให้ใส่ใจกับการตั้งค่าของ entype เมื่อส่งแบบฟอร์ม:
<! doctype html> <html> <head> <meta http-equiv = "content-type" content = "text/html; charset = utf-8"/> </head> <body> <form action = " <อินพุตประเภท = "ส่ง" value = "ส่ง"/> </form> </body> </html>
อัปโหลดรหัส Java พื้นหลังที่เกี่ยวข้อง สำหรับคำถามเฉพาะโปรดดูความคิดเห็น:
ตัวควบคุมแพ็คเกจ นำเข้า Java.io.File; นำเข้า java.io.ioException; นำเข้า java.util.iterator; นำเข้า javax.servlet.http.httpservletrequest; นำเข้า org.apache.commons.io.fileutils; นำเข้า org.springframework.http.httpheaders; นำเข้า org.springframework.http.httpstatus; นำเข้า org.springframework.http.mediatype; นำเข้า org.springframework.http.responseentity; นำเข้า 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.CommonsMultipartResolver; @Controller คลาสสาธารณะ FileController { /** * ไฟล์อัปโหลด * * @author: tuzongxun * @title: uploadfile * @param @param ไฟล์ * @param @param คำขอ * @param @throws @RequestMapping (value = "/uploadfile.do", method = requestMethod.post) โมฆะสาธารณะ uploadfile (คำขอ httpservletrequest) พ่น regenlstateException, ioexception {// @requestparam ("ไฟล์") request.getSession (). getServletContext ()); // ตรวจสอบว่าคำขอมีการอัปโหลดไฟล์นั่นคือถ้า (MultiPartResolver.ismultipart (คำขอ)) {// แปลงเป็น MultipARTENT Request MultiparthttpServletRequest Multirequest = (MultiparthttpServletRequest) // รับชื่อไฟล์ทั้งหมดในคำขอ iterator <string> iter = multirequest.getFilenames (); ในขณะที่ (iter.hasnext ()) {// รับไฟล์อัพโหลด multipartfile f = multirequest.getFile (iter.next ()); if (f! = null) {// รับชื่อไฟล์ของสตริงไฟล์ที่อัปโหลดในปัจจุบัน myfilename = f.getoriginalfilename (); // ถ้าชื่อไม่ใช่ "" หมายความว่าไฟล์นั้นมีอยู่มิฉะนั้นหมายความว่าไฟล์ไม่มีอยู่ถ้า (myfilename.trim ()! = "") {// กำหนดเส้นทางสตริงการอัปโหลดพา ธ = "c: // ผู้ใช้ // tuzongxun123 // desktop //" + myfilename; ไฟล์ localfile = ไฟล์ใหม่ (พา ธ ); f.transferto (localfile); -หลังจากเลือกไฟล์ที่จะส่งคุณจะเห็นว่าไฟล์ที่เลือกจะถูกส่งไปยังตำแหน่งที่ระบุในรหัส การแสดงผลหน้ามีดังนี้
3. ดาวน์โหลดไฟล์
การดาวน์โหลดไฟล์ต้องใช้เส้นทางการจัดเก็บของไฟล์ที่ดาวน์โหลด นี่เป็นเพียงการกรอกข้อมูลด้วยตนเองหากอยู่ในโครงการเฉพาะคุณสามารถบันทึกชื่อไฟล์และเส้นทางการจัดเก็บที่อัปโหลดในฐานข้อมูล จากนั้นเพิ่มหน้าของรายการไฟล์เพื่อแสดงชื่อไฟล์และเส้นทางไฟล์จากนั้นคลิกเพื่ออัปโหลดชื่อไฟล์ที่เกี่ยวข้องและเส้นทางไปยังการดำเนินการพื้นหลังเมื่อดาวน์โหลด
/ ** * ดาวน์โหลดไฟล์ชื่อไฟล์และที่อยู่ไฟล์ที่ต้องการ * * @author: tuzongxun * @title: ดาวน์โหลด * @param @param name * @param @param path * @param @return * @param @throws ioexception * @returnressentity <byte [] "/downloadfile.do") Public ResponseEntity <byte []> ดาวน์โหลด (@requestparam ("ชื่อ") ชื่อสตริง, @requestparam ("filepath") เส้นทางสตริง) พ่น IOException {ไฟล์ไฟล์ = ไฟล์ใหม่ (พา ธ ); ส่วนหัว httpheaders = httpheaders ใหม่ (); String filename = new String (name.getBytes ("UTF-8"), "ISO-8859-1"); // เพื่อแก้ปัญหาส่วนหัวของชื่อภาษาจีน Headers.setContentType (MediaType.Application_octet_stream); ส่งคืน ResponseEntity ใหม่ <byte []> (fileutils.readFileTobyTearray (ไฟล์), ส่วนหัว, httpstatus.created); - หน้า HTML เป็นเพียงการกรอกข้อมูลง่าย ๆ ในชื่อไฟล์และเส้นทางไฟล์และส่งไปยังพื้นหลังโดยใช้แบบฟอร์มแบบฟอร์ม จากนั้นพื้นหลังจะควบคุมการตอบสนองไปยังกล่องการเลือกสำหรับการบันทึก Popup Path File บนหน้า แน่นอนฉันไม่ได้ทำอะไรในพื้นหลังที่นี่ หากไฟล์ไม่มีอยู่จะมีการรายงานข้อผิดพลาด คุณสามารถดำเนินการประมวลผลที่สอดคล้องกัน:
ดาวน์โหลดไฟล์:
</br> </br> <form action = "./ downloadfile.do" style = "border: 1px solid grey; width: auto;" method = "post"> ชื่อไฟล์: <input type = "text" name = "name"/> </br> </br> เส้นทางไฟล์: <อินพุต type = "text" name = "filePath"/> </br> </br> <อินพุต type = "submit" value = "ยืนยันการดาวน์โหลด"
มุมมองหน้ามีดังนี้:
หากไฟล์ไม่มีอยู่ข้อผิดพลาดจะมีดังนี้:
ข้างต้นเป็นเรื่องเกี่ยวกับบทความนี้ฉันหวังว่ามันจะเป็นประโยชน์กับการเรียนรู้ของทุกคน