1. สร้างสภาพแวดล้อม struts2
ภายใต้ MyEclipse โครงการคลิกขวา -> MyEclipse -> facets โครงการ -> ติดตั้ง Apache struts2
หากคุณต้องการสร้างมันเองคุณต้องดาวน์โหลดแพ็คเกจ Struts2 และเขียนไฟล์การกำหนดค่า struts.xml
การกำหนดค่าไฟล์ web.xml มีดังนี้:
<Tilter> <Tilter-Name> struts2 </tilter-name> <silter-class> org.apache.struts2.dispatcher.ng.filter.strutspreprepareedexecutefilter </filter-class> </filter> </ตัวกรองการทำแผนที่>
2. การอัปโหลดไฟล์
1. หน้าแผนกต้อนรับ:
หน้าอัปโหลด:
<body> <form action = "upload.action" method = "post" enctype = "multipart/form-data"> <input type = "file" name = "upload"/> <input type = "ส่ง" value = "ส่ง"/> <br> $ {ผลลัพธ์} </form> </body>แอตทริบิวต์ชื่ออินพุตสอดคล้องกับการตั้งชื่อพื้นหลัง
อัปโหลดหน้าล้มเหลว:
<body> <h2> การอัปโหลดล้มเหลว </h2> <s: Fielderror> </s: Fielderror> </body>
ความต้องการ:
<%@ taglib uri = "/struts-tags" คำนำหน้า = "s"%>
2. การกระทำแบ็กเอนด์
คุณสมบัติหลักอัปโหลด, uploadContentType, uploadfilename
แพ็คเกจ com.yf.action; นำเข้า java.io.file; นำเข้า org.apache.commons.io.fileutils; นำเข้า org.apache.struts2.servletactionContext; นำเข้า com.opensymphony.xwork2.actionsupport; การอัปโหลดระดับสาธารณะ สตริงส่วนตัว uploadContentType; uploadfilename สตริงส่วนตัว; ผลสตริงส่วนตัว ไฟล์สาธารณะ getUoLoad () {return upload; } โมฆะสาธารณะ setupload (อัปโหลดไฟล์) {this.upload = อัปโหลด; } สตริงสาธารณะ getUploadContentType () {return uploadContentType; } โมฆะสาธารณะ setuploadContentType (สตริง uploadContentType) {this.uploadContentType = uploadContentType; } สตริงสาธารณะ getUploadFileName () {return uploadfilename; } โมฆะสาธารณะ setUploadFileName (สตริง updoadFileName) {this.uploadFileName = uploadFileName; } สตริงสาธารณะ getResult () {ผลตอบแทน; } โมฆะสาธารณะ setResult (ผลลัพธ์สตริง) {this.result = ผลลัพธ์; } @Override สตริงสาธารณะ execute () พ่นข้อยกเว้น {String path = servletActionContext.getServletContext (). getRealPath ("/images"); ไฟล์ไฟล์ = ไฟล์ใหม่ (พา ธ ); if (! file.exists ()) {file.mkdir (); } system.out.println (อัปโหลด); fileutils.copyfile (อัปโหลด, ไฟล์ใหม่ (ไฟล์, uploadfilename)); result = "อัปโหลดสำเร็จ"; กลับมาประสบความสำเร็จ -3.STRUTS.XML การกำหนดค่าไฟล์
กำหนดค่าการกระทำและกำหนดค่าตัวดักจับเพื่อ จำกัด ประเภทและขนาดของไฟล์ที่อัปโหลด
<? XML เวอร์ชัน = "1.0" การเข้ารหัส = "UTF-8"?> <! Doctype struts สาธารณะ "-// Apache Software Foundation // dtd struts การกำหนดค่า 2.1 // en" "http://struts.apache.org/dtds/struts-2.1.dtd" value = "false"/> <constant name = "struts.devmode" value = "true"/> <constant name = "struts.multipart.savedir" value = "/tmp"/> <constant name = "struts.custom.i18n.resources" value = "app" name = "upload"> <result> /index.jsp </result> <result name = "input">/orlan.jsp </result> <!-กำหนดค่า interceptor เพื่อ จำกัด ประเภทและขนาดของไฟล์ที่อัปโหลด-> <interceptor-ref name = "fileupload"> <param name = "image image/bmp name = "maximumsize"> 2m </param> </interceptor-ref> <interceptor-ref name = "defaultStack"> </interceptor-ref> </action>
4. สร้างไฟล์คุณสมบัติใหม่
ข้อมูลการอัปโหลดไฟล์ล้มเหลวจะปรากฏขึ้นที่เบื้องหน้าและข้อความแสดงข้อผิดพลาดจะปรากฏขึ้นเมื่อทำการประมวลผล
เนื้อหาไฟล์มีดังนี้:
struts.messages.error.file.too/ใหญ่ =/u4e0a/u4f20/u6587/u4ef6/u592a/u5927/u4e86/uff01struts.m Essages.error.content.type.not.allowed =/U4E0A/U4F20/U6587/U4EF6/U7C7B/U578B/U4E0D/U7B26/UFF01
นั่นคือการเพิ่ม:
ชื่อ: struts.messages.error.file.too/ใหญ่
ค่า: ไฟล์อัปโหลดมีขนาดใหญ่เกินไป!
ชื่อ: struts.messages.error.content.type.not.allowed
ค่า: ประเภทไฟล์ที่อัปโหลดไม่ตรงกัน!
ผลการดำเนินการมีดังนี้:
เลือกอิมเมจ jpg ขนาดไม่เกิน 2m หลังจากทำงาน
เลือกไฟล์ที่ไม่ใช่ภาพ:
หากคุณต้องการอัปโหลดไฟล์ในแบตช์ให้เปลี่ยนการอัปโหลดพื้นหลังอัพโหลด uploadContentType และ uploadfilename เป็นรายการอ่านไฟล์อัปโหลดในลูปและบันทึกลงในฮาร์ดดิสก์เพิ่มอินพุตลงในแผนกต้อนรับและแอตทริบิวต์ชื่อเหมือนกัน
ข้างต้นคือการวิเคราะห์ตัวอย่างของฟังก์ชั่นการอัปโหลดไฟล์ Struts2 ที่แนะนำโดยตัวแก้ไข ฉันหวังว่ามันจะเป็นประโยชน์กับทุกคน หากคุณมีคำถามใด ๆ โปรดฝากข้อความถึงฉัน บรรณาธิการจะตอบกลับทุกคนในเวลา!