1. แนวคิดสำหรับการอัปโหลดไฟล์ Struts
ฉันเคยพูดคุยเกี่ยวกับส่วนประกอบ FilePload มาก่อน มันมีฟังก์ชั่นที่ทรงพลัง แต่การดำเนินการมีความซับซ้อนและซับซ้อน เวลานี้การอัปโหลดไฟล์ยังได้รับการสนับสนุนในความเข้มงวดและ FilePload ได้รับการบรรจุซึ่งสะดวกกว่าในการใช้งานมาก
ที่นี่มีแท็กและคลาส:
<html: file property = "สอดคล้องกับชื่อคุณสมบัติใน actionform">
ใช้แท็กนี้เพื่ออัปโหลดไฟล์ แต่เพื่อรับเนื้อหาคุณต้องพึ่งพา org.apache.struts.upload.formfile
อินเทอร์เฟซเสร็จสมบูรณ์
2. การรับรู้
sh.jsp:
<%@ page language = "java" import = "java.util.*" pageencoding = "utf-8"%> <%@ taglib uri = "http://jakarta.apache.org/struts/tags-bean" prefix = "Bean"%@ taglib uri = "http://jakarta.apache.org/struts/tags-html" คำนำหน้า = "html"%> <%@ taglib uri = "http://jakarta.apache.org/struts/tags-logic" prefix = "logic html 4.01 transitional // en "> <html> <head> <title> sh.jsp </title> <meta http-equiv =" content-type "เนื้อหา =" ข้อความ/html; enctype = "multipart/form-data"> <html: file property = "photo"> </html: ไฟล์> <html: ส่ง value = "upload"> </html: ส่ง> </html: แบบฟอร์ม> </body> </html>
uploadform.java:
แพ็คเกจ com.zyy.struts.form; นำเข้า org.apache.struts.action.actionform; นำเข้า org.apache.struts.upload.formfile; คลาสสาธารณะอัพโหลดฟอร์ฟฟอร์มขยาย ActionForm {ภาพถ่ายฟอร์มไฟล์ส่วนตัว; รูปแบบสาธารณะ getphoto () {return photo; } โมฆะสาธารณะ setphoto (รูปแบบฟอร์มไฟล์) {this.photo = photo; - iptimestamp.java:
แพ็คเกจ com.zyy.util; นำเข้า java.text.simpledateFormat; นำเข้า java.util.date; นำเข้า java.util.random; คลาสสาธารณะ iptimestamp {ส่วนตัว SimpledateFormat SDF = NULL; สตริงส่วนตัว ip = null; public iptimestamp () {} สาธารณะ iptimestamp (สตริง ip) {this.ip = ip; } สตริงสาธารณะ getIptimerand () {StringBuffer buf = new StringBuffer (); if (this.ip! = null) {string s [] = this.ip.split ("//."); สำหรับ (int i = 0; i <s.length; i ++) {buf.append (this.addzero (s [i], 3)); }} buf.append (this.getTimestamp ()); สุ่ม r = ใหม่สุ่ม (); สำหรับ (int i = 0; i <3; i ++) {buf.append (r.nextint (10)); } return buf.toString (); } สตริงสาธารณะ getDate () {this.sdf = ใหม่ simpledateFormat ("yyyy-mm-dd hh: mm: ss.sss"); ส่งคืน this.sdf.format (วันที่ใหม่ ()); } สตริงสาธารณะ getTimestamp () {this.sdf = ใหม่ simpledateFormat ("yyyymmddhhmmsssssss"); ส่งคืน this.sdf.format (วันที่ใหม่ ()); } สตริงส่วนตัว addzero (สตริง str, int len) {stringbuffer s = new StringBuffer (); S.Append (STR); ในขณะที่ (s.length () <len) {s.insert (0, "0"); } return s.toString (); } โมฆะคงที่สาธารณะหลัก (สตริง args []) {system.out.println (ใหม่ iptimestamp ("192.168.1.1"). getiptimerand ()); -uploadaction.java:
แพ็คเกจ com.zyy.struts.action; นำเข้า java.io.file; นำเข้า java.io.fileoutputstream; นำเข้า java.io.InputStream; นำเข้า java.io.OutputStream; นำเข้า Javax.servlet.http.httpservletRequest; org.apache.struts.action.action; นำเข้า org.apache.struts.action.actionform; นำเข้า org.apache.structs.Action.Actionforward นำเข้า org.apache.structs.Action.ActionMapping; นำเข้า com.zyy.structs.form.uploadform; คำขอ httpservletRequest, การตอบสนอง httpservletResponse) พ่นข้อยกเว้น {uploadform uploadform = (uploadform) แบบฟอร์ม; iptimestamp ips = ใหม่ iptimestamp (request.getRemoteaddr ()); // ชื่อสตริงชื่อไฟล์ filename = ips.getiptimerand () + " + uploadform.getphoto (). getFilename (). แยก ("//.") [uploadform.getphoto (). getFilename (). แยก ("//."). ความยาว - 1]; // เอาต์พุตพา ธ ไฟล์ outfile = ไฟล์ใหม่ (super.getServlet (). getServletContext () .getRealPath ("/") + "อัปโหลด" + file.Separator + ชื่อไฟล์); // โฟลเดอร์ที่ไฟล์ถูกเก็บไว้ไฟล์ = ไฟล์ใหม่ (super.getServlet (). getServletContext () .getRealPath ("/") + "อัปโหลด"); if (! file.exists ()) {file.mkdir (); } inputStream input = uploadform.getphoto (). getInputStream (); OutputStream output = ใหม่ fileOutputStream (outfile); ข้อมูลไบต์ [] = ไบต์ใหม่ [1024]; int temp = 0; ในขณะที่ ((temp = input.read (data, 0, 1024))! = -1) {output.write (ข้อมูล); } output.close (); input.close (); คืนค่า null; -struts-config.xml:
<? xml เวอร์ชัน = "1.0" การเข้ารหัส = "iso-8859-1"?> <! doctype struts-config สาธารณะ "-// Apache Software Foundation // dtd struts การกำหนดค่า 1.2 // en" "http://jakarta.org/struts/dtds <ฟอร์ม-Beans> <form-bean name = "uploadform" type = "com.zyy.struts.form.uploadform"> </form-bean> </form-beans> <การรับรู้ทั่วโลก> </global-exceptions> name = "uploadform" scope = "request" type = "com.zy.struts.action.uploadaction"> </action> </action-mappings> <message-resources parameter = "Resource.MessageSources"/> </struts-config>
เนื่องจากสิ่งที่ฉันกำลังบันทึกคือ super.getServlet (). getServletContext (). getRealPath ("/")+ "อัปโหลด"
นี่คือเส้นทางที่แท้จริงของไดเรกทอรีเสมือนจริงภายใต้โฟลเดอร์อัปโหลด
จะเห็นได้ว่าหลักการของการอัปโหลดไฟล์ใน struts นั้นเหมือนกับ FilePload แต่มีการบรรจุ struts ดังนั้นจึงเห็นได้ชัดว่าสะดวกกว่าการใช้งานมากกว่าการใช้ส่วนประกอบ fileupload เพียงอย่างเดียว
ข้างต้นเป็นเนื้อหาทั้งหมดของบทความนี้ ฉันหวังว่ามันจะเป็นประโยชน์ต่อการเรียนรู้ของทุกคนและฉันหวังว่าทุกคนจะสนับสนุน wulin.com มากขึ้น