تشارك هذه المقالة معك طريقة تنفيذ تحميل الملفات من إطار STRUTS2 للرجوع إليه. المحتوى المحدد كما يلي
عملية تكوين Struts2
(1) أضف حزمة جرة إلى المشروع
(2) تكوين مرشح (مرشح) في web.xml
<؟ XSI: schemalocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web- <filter-name> struts2 </filter-name> <ilter-class> org.apache.struts2 </filter-mapping> </bil-app>
(3) ملف تكوين struts.xml
<؟ الإصدار = "2.5" xmlns = "http://java.sun.com/xml/ns/javaee" xmlns: xsi = "http://www.w3.org/2001/xmlschema-instance ON = "http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <display-name> </isplay-name> <ilter > <filter-name> struts2 </filter-name> <ilter-class> org.apache.struts2.dispatcher.ng.filter.strutsprepareexecutefilter </filt er-class> </filter> <filter-mapping> <filter-name> Struts2 </filter-name> <Url-pattern>/*</url-pattern> </filter-mapping> </web-app>
(4) كتابة فئة العمل
حزمة com.xmgc.sc.Action ؛ استيراد java.io.file ؛ استيراد java.io.fileinputStream ؛ استيراد java.io.fileOutputStream ؛ استيراد java.io.ioException ؛ استيراد org.apache.struts2.ServleTActionContext ؛ الطبقة العامة myuploadAction {عنوان السلسلة الخاصة ؛ تحميل الملف الخاص ؛ // اسم نوع الملف في النموذج هو نفس السلسلة الخاصة uploadContentType ؛ // يجب أن يكون البادئة هو تحميل السلسلة الخاصة أعلاه ؛ السلسلة العامة getTitle () {return title ؛ } public void settitle (tring title) {this.title = title ؛ } الملف العام getUpload () {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 ؛ } /* public string getSavePath () {// servletContext cxt = servletactionContext.getServletContext () ؛ // string path = cxt.getRealPath ("/") ؛ // هذا يحصل على المسار هو: http: // localhost: 8080/sc // بعد التحميل ، يصبح e: /software/apache-tomcat-6.0.45/webapps/sc return savepath ؛ } public void setSavePath (string savepath) {//e:/software/apache-tomcat-6.0.45/webapps/sc/myupload this.savepath = sactleCtactionContext.getServletContext (). getRealPath ("/myupload") ؛ }*/string public execute () يلقي ioException {system.out.println (title) ؛ // title system.out.println (UploadContentType) ؛ // نوع ملف الملف المراد تحميله. String RealPath = servleTactionContext.getServletContext (). getRealPath ("/") ؛ string path = RealPath+"myupload/"+uploadFilename ؛ System.out.println (RealPath) ؛ system.out.println (path) ؛ FileInputStream fis = جديد fileInputStream (تحميل) ؛ fileOutputStream fos = new FileOutputStream (path) ؛ byte [] bytes = new byte [1024] ؛ // تحديد مجموعة بايت من 1024 حجم int len = -1 ؛ // استخدم لاستخدامها كبوت العلم بينما ((len = fis.read (bytes))> 0) {fos.write (bytes ، 0 ، len) ؛ } إرجاع فارغ ؛ }}(5) كتابة صفحات JSP
<٪@ page contentType = "text/html ؛ charset = utf-8" ٪> <!-enctype = "multipart/form-data" ، هذا هو أهم تكوين-> <form action = "myupload.action" enctype = "multipart/form-data" name = "upload"/> <br/> <إدخال type = "إرسال" value = "expload"/> </form>
بعد هذا الملخص ، أشعر أن تحميل ملف واحد تحت إطار STRUTS2 لا يزال بسيطًا للغاية. ما عليك سوى الحصول على العنوان ليتم تخزينه ، ثم اكتبه إلى هذا العنوان من خلال دفق الإدخال والإخراج. بالنسبة لمعظم العمل ، قام Struts2 بذلك من أجلنا.
ما سبق هو كل محتوى هذه المقالة. آمل أن يكون ذلك مفيدًا لتعلم الجميع وآمل أن يدعم الجميع wulin.com أكثر.