1. بناء بيئة Struts2
تحت myeclipse ، انقر بزر الماوس الأيمن فوق مشروع -> myeclipse -> جوانب المشروع -> تثبيت apache Struts2.
إذا كنت ترغب في بنائه بنفسك ، فأنت بحاجة إلى تنزيل حزمة Struts2 وكتابة ملف تكوين Struts.xml.
تكوين ملف web.xml كما يلي:
<Ivilter> <filter-name> struts2 </filter-name> <ilter-class> org.apache.struts2 </filter mapping>
2. تحميل الملف
1. صفحة مكتب الاستقبال:
صفحة التحميل:
<Body> <form Action = "upload.action" method = "post" enctype = "multipart/form-data"> <input type = "file" name = "upload"/> <input type = "pridt" value =
تتوافق سمة اسم الإدخال مع تسمية الخلفية.
الصفحة الفاشلة التحميل:
<Body> <h2> فشل تحميل </h2> <s: fielderror> </s: fielderror> </body>
يحتاج:
<٪@ taglib uri = "/struts-tags" prefix = "s" ٪>
2. العمل الخلفي
الخصائص الرئيسية تحميل ، uploadContentType ، exploadFilename.
package com.yf.action ؛ import java.io.file ؛ import org.apache.commons.io.fileutils ؛ import org.apache.struts2 سلسلة خاصة exploadContentType ؛ سلسلة خاصة تحميل FloadFilename ؛ نتيجة سلسلة خاصة الملف العام getUoload () {return upload ؛ } public void setupload (تحميل الملف) {this.upload = upload ؛ } السلسلة العامة getUploAdContentType () {return UploadContentType ؛ } public void setuploadContentType (سلسلة uploadContentType) {this.uploadContentType = UploadContentType ؛ } السلسلة العامة getUploAdfilename () {return UploadFilename ؛ } public void setuploadfilename (سلسلة exploadFiLename) {this.uploadfilename = UploadFilename ؛ } سلسلة عامة getResult () {return return ؛ } public void setResult (String result) {this.result = result ؛ } Override Public String Execute () يلقي استثناء {String path = servleTactionContext.getServletContext (). getRealPath ("/Images") ؛ ملف الملف = ملف جديد (مسار) ؛ if (! file.exists ()) {file.mkdir () ؛ } system.out.println (تحميل) ؛ FileUtils.copyFile (تحميل ، ملف جديد (ملف ، eploadFiLename)) ؛ النتيجة = "تم تحميلها بنجاح" ؛ العودة النجاح }}3.struts.xml تكوين ملف
قم بتكوين الإجراء وتكوين Interceptor للحد من نوع وحجم الملفات التي تم تحميلها.
<؟ value = "false"/> <name name = "struts.devmode" value = "true"/> <name constant = "struts.multipart.savedir" value = "/tmp"/> <constant name = "struts.custom.i18n.Resources" value = " name = "expload"> <desult> /index.jsp </redress> <result name = "input">/error.jsp </result> <!-تكوين اعتراض للحد من نوع وحجم الملفات التي تم تحميلها-> <interceptor-ref name = "fileupload" name = "maximumsize"> 2m </param> </stilection-ref> <interceptor-ref name = "defaultStack"> </interceptor-ref> </sociat
4. إنشاء ملف خصائص جديد
يتم عرض المعلومات الفاشلة لتحميل الملف إلى المقدمة ، ويتم عرض رسالة الخطأ عند المعالجة.
محتويات الملف كما يلي:
struts.messages.error.file.too/large =/u4e0a/u4f20/u6587/u4ef6/u592a/u5927/u4e86/uff01struts.m Essges.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 ، لا يتجاوز الحجم 2 متر ، بعد التشغيل
حدد ملف غير صور:
إذا كنت بحاجة إلى تحميل الملفات على دفعات ، قم بتغيير التحميل الخلفي ، و uploadContentType ، و exploadFilename لإدراجها ، وقراءة ملف التحميل في حلقة وحفظه على القرص الثابت ، وإضافة إدخال إلى مكتب الاستقبال ، وسممة الاسم هي نفسها.
ما ورد أعلاه هو تحليل مثال لوظيفة تحميل ملف Struts2 التي أدخلها المحرر. آمل أن يكون ذلك مفيدًا للجميع. إذا كان لديك أي أسئلة ، يرجى ترك رسالة لي. سوف يرد المحرر على الجميع في الوقت المناسب!