สปริง MVC อัปโหลดและดาวน์โหลดดังนี้:
(1) นำเข้าแพ็คเกจ Jar: Ant.jar, Commons-fileupload.jar, connom-io.jar
(2) เพิ่มใน src/context/dispatcher.xml
<bean id = "multipartresolver" p: defaultencoding = "utf-8" />
โปรดทราบว่าคุณต้องเพิ่มเนื้อหาลงในหัวดังที่แสดงด้านล่างหลังจากเพิ่ม:
<ถั่วเริ่มต้น-lazy-init = "true" xmlns = "http://www.springframework.org/schema/beans" xmlns: p = "http://www.springframework.org/schema/p" xmlns: xsi = "http://www.w3.org/2001/xmlschema-instance" xmlns: context = "http://www.springframework.org/schema/context" xmlns: mvc = "http://www.springframework.org/schema/mvc" xsi: schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/mvc http://ww.springframework.org/schema http://www.springframework.org/schema/context/spring-context-3.0.xsd ">
(3) เพิ่มคลาสเครื่องมือ FileOperateUtil.java
/ ** * * @author geoin */ แพ็คเกจ com.geloin.spring.util; นำเข้า Java.io.BufferedInputStream; นำเข้า Java.io.BufferedOutputStream; นำเข้า Java.io.File; นำเข้า Java.io.FileInputStream; นำเข้า Java.io.FileOutputStream; นำเข้า java.text.simpledateFormat; นำเข้า java.util.arraylist; นำเข้า java.util.date; นำเข้า java.util.hashmap; นำเข้า java.util.iterator; นำเข้า java.util.list; นำเข้า java.util.map; นำเข้า javax.servlet.http.httpservletrequest; นำเข้า Javax.servlet.http.httpservletResponse; นำเข้า org.apache.tools.zip.zipentry; นำเข้า org.apache.tools.zip.zipoutputstream; นำเข้า org.springframework.util.filecopyutils; นำเข้า org.springframework.web.multipart.multipartfile; นำเข้า org.springframework.web.multipart.multiparthttpservletrequest; คลาสสาธารณะ FileOperateUtil {สตริงสุดท้ายคงที่ realName = "realName"; สตริงสุดท้ายคงที่ส่วนตัว storename = "storename"; ขนาดสตริงสุดท้ายคงที่ส่วนตัว = "ขนาด"; สตริงสุดท้ายคงที่ส่วนตัวต่อท้าย = "คำต่อท้าย"; สตริงสุดท้ายคงที่ส่วนตัว ContentType = "ContentType"; สตริงสุดท้ายคงที่ private createTime = "createTime"; สตริงสุดท้ายคงที่ส่วนตัว uploaddir = "uploaddir/"; / ** * เปลี่ยนชื่อไฟล์ที่อัปโหลด * * @param ชื่อ * @return */ การเปลี่ยนชื่อสตริงแบบคงที่ส่วนตัว (ชื่อสตริง) {long now = long.parselong (ใหม่ SimpledateFormat ("yyyymmddhhmmss") .format (วันที่ใหม่ ()); long random = (ยาว) (math.random () * ตอนนี้); String filename = Now + "" + สุ่ม; if (name.indexof (".")! = -1) {filename += name.substring (name.lastindexof (".")); } ส่งคืนชื่อไฟล์; } / ** * ชื่อไฟล์บีบอัด * * @param ชื่อ * @return * / สตริงคงที่ส่วนตัว zipname (ชื่อสตริง) {สตริงคำนำหน้า = ""; if (name.indexof (".")! = -1) {prefix = name.substring (0, name.lastindexof (".")); } else {prefix = name; } ส่งคืนคำนำหน้า + ".zip"; } / ** * อัปโหลดไฟล์ * * @param Request * @param params * @param ค่า * @return * @throws Exception * / รายการคงที่สาธารณะ <แผนที่ <String <String, Object >> อัปโหลด (httpservletRequest request, string [] พารามิเตอร์, แผนที่ <สตริง, วัตถุ []> ค่า) multiparthttpservletRequest mrequest = (multiparthttpservletrequest) คำขอ; แผนที่ <string, multipartfile> fileMap = mrequest.getFileMap (); String uploaddir = request.getSession (). getServletContext () .getRealPath ("/") + fileOperateUtil.uploaddir; ไฟล์ไฟล์ = ไฟล์ใหม่ (uploaddir); if (! file.exists ()) {file.mkdir (); } string filename = null; int i = 0; สำหรับ (iterator <map.entry <string, multipartFile >> it = fileMap.entrySet () .Iterator (); it.hasNext (); i ++) {map.entry <string, multipartfile> entnt.next (); multipartFile mfile = entry.getValue (); filename = mfile.getoriginalfilename (); String storename = Rename (ชื่อไฟล์); สตริง nozipname = uploaddir + storename; string zipname = zipname (nozipname); // อัปโหลดเพื่อเป็นไฟล์บีบอัด zipoutputStream outputStream = new ZipOutputStream (ใหม่ bufferedOutputStream (ใหม่ fileOutputStream (zipname))); outputStream.putNextEntry (zipentry ใหม่ (ชื่อไฟล์)); OutputStream.SetEncoding ("GBK"); fileCopyutils.copy (mfile.getInputStream (), outputstream); แผนที่ <string, Object> map = new hashmap <string, object> (); // แก้ไขค่าพารามิเตอร์คู่ map.put (fileOperateUtil.RealName, zipname (ชื่อไฟล์)); map.put (fileOperateutil.storename, zipname (storename)); map.put (fileOperateUtil.size, ไฟล์ใหม่ (zipname) .length ()); map.put (fileOperateUtil.suffix, "zip"); map.put (fileOperateutil.contentType, "แอปพลิเคชัน/octet-stream"); map.put (fileOperateUtil.createTime, วันที่ใหม่ ()); // คู่พารามิเตอร์ที่กำหนดเองคู่สำหรับ (พารามิเตอร์สตริง: พารามิเตอร์) {map.put (param, value.get (param) [i]); } result.add (แผนที่); } ผลตอบแทนผลลัพธ์; } / ** * ดาวน์โหลด * @param Request * @param Response * @param storename * @param contentType * @param realname * @throws Exception * / โมฆะคงที่สาธารณะดาวน์โหลด (httpservletrequest Request Response.setContentType ("ข้อความ/html; charset = utf-8"); request.Setcharacterencoding ("UTF-8"); bufferedInputStream bis = null; bufferedOutputStream bos = null; String ctxPath = request.getSession (). getServletContext () .getRealPath ("/") + fileOperateTil.uploaddir; String downloadPath = ctxPath + storename; Long Filelength = ไฟล์ใหม่ (downloadPath) .length (); Response.SetContentType (ContentType); Response.SetheAder ("เนื้อหา-การจัดสรร", "เอกสารแนบ; filename =" + สตริงใหม่ (realname.getBytes ("UTF-8"), "iso8859-1")); Response.Setheader ("ความยาวเนื้อหา", string.valueof (filelength)); bis = ใหม่ bufferedInputStream (ใหม่ FileInputStream (downloadPath)); BOS = ใหม่ bufferedOutputStream (response.getOutputStream ()); ไบต์ [] buff = ไบต์ใหม่ [2048]; int bytesread; ในขณะที่ (-1! = (bytesRead = bis.read (buff, 0, buff.length)) {bos.write (buff, 0, bytesRead); } bis.close (); bos.close (); - สามารถใช้งานได้อย่างสมบูรณ์โดยไม่ต้องเปลี่ยนชั้นเรียน ควรสังเกตว่าไฟล์ที่อัปโหลดอยู่ภายใต้ WebContent/UploadDir
(4) เพิ่ม FileOperAtecontroller.java
/ ** * * @author geoin */ แพ็คเกจ com.geloin.spring.controller; นำเข้า java.util.hashmap; นำเข้า java.util.list; นำเข้า java.util.map; นำเข้า javax.servlet.http.httpservletrequest; นำเข้า Javax.servlet.http.httpservletResponse; นำเข้า org.springframework.stereotype.controller; นำเข้า org.springframework.web.bind.servletrequestutils; นำเข้า org.springframework.web.bind.annotation.requestmapping; นำเข้า org.springframework.web.servlet.modelandview; นำเข้า com.geloin.spring.util.fileoperateutil; @Controller @RequestMapping (value = "พื้นหลัง/fileOperate") คลาสสาธารณะคลาส FileOperAtecontroller {/*** ไปยังตำแหน่งที่ไฟล์ถูกอัปโหลด* @return*/@requestmapping (value = "to_upload") โมเดลสาธารณะ } / ** * อัปโหลดไฟล์ * * @param Request * @return * @throws Exception * / @requestmapping (value = "upload") โมเดลสาธารณะและการอัปโหลด (httpservletrequest คำขอ) โยนข้อยกเว้น {แผนที่ <สตริง // นามแฝงสตริง [] alaises = servletrequestutils.getStringParameters (คำขอ, "alais"); สตริง [] params = สตริงใหม่ [] {"alais"}; แผนที่ <string, object []> values = new hashmap <string, object []> (); value.put ("Alais", Alaises); รายการ <map <string, object >> result = fileOperateUtil.upload (คำขอ, params, ค่า); map.put ("ผลลัพธ์" ผลลัพธ์); ส่งคืน ModelandView ใหม่ ("พื้นหลัง/fileOperate/list", แผนที่); } / ** * ดาวน์โหลด * * @Param Attachment * @param Request * @param Response * @return * @throws Exception * / @requestmapping (value = "ดาวน์โหลด") Public ModelandView ดาวน์โหลด (httpservletRequest Regement, httpservletResponse) String realName = "Java Design Pattern.zip"; String ContentType = "Application/Octet-Stream"; fileOperateUtil.download (คำขอ, การตอบสนอง, storename, contentType, realname); คืนค่า null; -โปรดเปลี่ยนวิธีการดาวน์โหลดด้วยตัวเอง หากคุณใช้ฐานข้อมูลเพื่อบันทึกข้อมูลไฟล์ที่อัปโหลดโปรดดูที่อินสแตนซ์การรวม MVC ของ Spring MVC
(5) เพิ่ม fileOperate/upload.jsp
<%@ page language = "java" contentType = "ข้อความ/html; charset = utf-8" pageencoding = "utf-8"%> <%@ taglib คำนำหน้า = "c" uri = "http://java.sun.com/jsp/jstl/core" 1.0 Transitional // en "" http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd "> <html> <head> <meta http-equiv =" content-type "content =" ข้อความ/html; <body> </body> <form enctype = "multipart /form-data" action = "<c: url value =" /พื้นหลัง /fileOperate /upload.html " />>" method = "post"> <input type = "file" name = "file1" /> name = "alais" /> <br /> <อินพุต type = "file" name = "file3" /> <input type = "text" name = "alais" /> <br /> <อินพุต type = "subment" value = "upload" /> < /form> </html>
ตรวจสอบให้แน่ใจว่าค่า Enctype เป็นแบบหลายส่วน/ฟอร์ม-ข้อมูล; ค่าวิธีการคือโพสต์
(6) เพิ่ม fileOperate/list.jsp
<%@ page language = "java" contentType = "ข้อความ/html; charset = utf-8" pageencoding = "utf-8"%> <%@ taglib คำนำหน้า = "c" uri = "http://java.sun.com/jsp/jstl/core" 1.0 Transitional // en "" http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd "> <html> <head> <meta http-equiv =" content-type "content =" ข้อความ/html; <body> <c: foreach items = "$ {result}" var = "item"> <c: foreach items = "$ {item}" var = "m"> <c: ถ้า test = "$ {m.key eq 'realname'}"> $ {m.value}(7) เข้าถึงหน้าอัปโหลดผ่าน http: // localhost: 8080/spring_test/พื้นหลัง/fileOperate/to_upload.html และดาวน์โหลดไฟล์ผ่าน http: // localhost: 8080/spring_test/พื้นหลัง/fileoperate/download.html
ข้างต้นเป็นเนื้อหาทั้งหมดของบทความนี้ ฉันหวังว่ามันจะเป็นประโยชน์ต่อการเรียนรู้ของทุกคนและฉันหวังว่าทุกคนจะสนับสนุน wulin.com มากขึ้น