ตัวแยกวิเคราะห์เริ่มต้นของ SpringMVC ไม่รวมตัวแยกวิเคราะห์สำหรับการอัปโหลดไฟล์ เมื่อใช้ SpringMVC เพื่ออัปโหลดไฟล์คุณจะต้องใช้การประกาศ MultipartResolver ที่จัดทำโดย SpringMVC เนื่องจาก CommonsMultipartResolver ใช้อินเทอร์เฟซ MultiPartResolver เราจึงสามารถกำหนดค่าได้ในไฟล์การกำหนดค่า SpringMVC ดังนี้:
<bean id = "multipartresolver"> <property name = "defaultEncoding" value = "utf-8" /> <property name = "maxuploadsize" value = "10485760000" /> <property name = "MaxinMemorySize" value = "40960" />
ก่อนอื่นแนะนำแพ็คเกจที่จำเป็นสำหรับการอัปโหลดไฟล์, การบันทึกคอมมอนส์-*. jar Commons-io-*. jar Commons-FilePileLoad-*. jar
สร้างหน้า JSP ใหม่
<%@ page language = "java" contentType = "ข้อความ/html; charset = utf-8" pageencoding = "utf-8"%> <! doctype html สาธารณะ "-// w3c // dtd html 4.01 transitional // en" <html> <head> <meta http-equiv = "content-type" content = "text/html; charset = utf-8"> <title> ไฟล์อัปโหลด </title> </head> <body> <%-<form action = "method uscy =" post "post" enctype = "multipart/forta method = "post" enctype = "multipart/form-data"> <input type = "file" name = "fileupload"/> <อินพุต type = "submit" value = "upload"/> </form> </body> </html>
มีหลายรูปแบบของการอัปโหลดไฟล์ SpringMVC ที่นี่ฉันจะแนะนำสองประเภท
ประเภทแรก ให้ดูที่คอนโทรลเลอร์
แพ็คเกจ gd.hz.springmvc.controller; นำเข้า Java.io.File; นำเข้า java.io.ioException; นำเข้า 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.Commons.CommonsMultipartFile; นำเข้า org.springframework.web.servlet.modelandview; @Controller ("userController") @requestmapping ("ผู้ใช้") คลาสสาธารณะ userController {// ไฟล์กระบวนการอัปโหลด @RequestMapping (value = "fileUpload", method = requestMethod.post) public modelandView FileupLoad (@requestparam ("fileupload") System.out.println (file.getContentType ()); // รับขนาดไฟล์ System.out.println (file.getSize ()); // รับชื่อไฟล์ System.out.println (file.getoriginalfilename ()); // ตรวจสอบว่า (! file.isempty ()) {String path = "D:/" + file.getoriginalfilename (); ไฟล์ localfile = ไฟล์ใหม่ (พา ธ ); ลอง {file.transferto (localfile); } catch (unlueLstateException e) {e.printStackTrace (); } catch (ioexception e) {e.printstacktrace (); }} ส่งคืน ModelandView ใหม่ ("DataSuccess"); -คลาส CommonsMultipartFile ให้วิธีการมากมายสำหรับการประมวลผลไฟล์ ตัวอย่างเช่นขนาดไฟล์อัปโหลดชื่อไฟล์ประเภทไฟล์และการใช้งานเฉพาะคุณสามารถดูเอกสารของ Spring ได้ Transferto เอาต์พุตไฟล์ไปยังสถานที่ที่ระบุ
วิธีที่สองของการอัปโหลดไฟล์วิธีนี้ใช้กันทั่วไป:
แพ็คเกจ gd.hz.springmvc.controller; นำเข้า Java.io.File; นำเข้า java.io.ioException; นำเข้า java.util.iterator; นำเข้า Javax.servlet.http.httpservletRequest; นำเข้า org.springframework.stereotype.controller; นำเข้า org.springframework.web.bind.annotation.requestmapping; นำเข้า org.springframework.web.bind.annotation.requestmethod; นำเข้า org.springframework.web.multipart.multipartfile; นำเข้า org.springframework.web.multipart.multiparthttpservletrequest; นำเข้า org.springframework.web.multipart.Commons.CommonsMultipartResolver; @Controller ("userController") @requestmapping ("ผู้ใช้") คลาสสาธารณะ userController {// ไฟล์กระบวนการอัปโหลด 2 @requestmapping (value = "fileupload2", method = requestmethod.post) สตริงสาธารณะ CommonsMultipartResolver MultipartResolver = ใหม่ CommonsMultipArtresolver (request.getSession (). getServletContext ()); // ตรวจสอบว่าฟอร์มมี enctype = "multipart/form-data" ถ้า (multipartresolver.ismultipart (คำขอ)) {MultiparthttpservletRequest Multirequest = (MultiparthttpservletRequest); Iterator <string> iter = multirequest.getFilenames (); ในขณะที่ (iter.hasnext ()) {// สืบทอดมาจาก CommonsmultipartFile เป็นเจ้าของวิธีการข้างต้น ไฟล์ multipartFile = multirequest.getFile (iter.next ()); if (file! = null) {string filename = "demoupload" + file.getoriginalfilename (); String Path = "D:/" + ชื่อไฟล์; ไฟล์ localfile = ไฟล์ใหม่ (พา ธ ); file.transferto (localfile); }} ส่งคืน "dataSuccess"; -MultiparthttpservletRequest ให้วิธีการที่ยืดหยุ่นมากขึ้นซึ่งสามารถรับไฟล์และชื่อไฟล์ได้หลายไฟล์และสามารถข้ามไปเพื่อรับไฟล์แต่ละไฟล์
ข้างต้นเป็นเนื้อหาทั้งหมดของบทความนี้ ฉันหวังว่ามันจะเป็นประโยชน์ต่อการเรียนรู้ของทุกคนและฉันหวังว่าทุกคนจะสนับสนุน wulin.com มากขึ้น