บทความนี้แบ่งปันวิธีการใช้การอัปโหลดไฟล์ของ Framework Struts2 กับคุณสำหรับการอ้างอิงของคุณ เนื้อหาเฉพาะมีดังนี้
กระบวนการกำหนดค่า struts2
(1) เพิ่มแพ็คเกจ Jar ลงในโครงการ
(2) การกำหนดค่าตัวกรอง (ตัวกรอง) ใน web.xml
<? xml version = "1.0" การเข้ารหัส = "utf-8"?> <web-app version = "2.5" xmlns = "http://java.sun.com/xml/ns/javaee" xmlns: xsi = "http://ww.w3.org/200 XSI: schemalocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_2_2_2_2_2_2 <Tilter-Name> struts2 </filter-name> <filter-class> org.apache.struts2.dispatcher.ng.filter.strutsprepareandexecutefilter </filter-class> </ilter> </ตัวกรอง-แมป> </eb-app>
(3) การเขียนไฟล์กำหนดค่า struts.xml
<? xml version = "1.0" encoding = "utf-8"?> <web-app เวอร์ชัน = "2.5" xmlns = "http://java.sun.com/xml/ns/javaee" xmlns: xsi = "http://www.w3.org/2001/xmlschema-instance" Xsi: Schemalocati on = "http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <display-name> > <filter-name> struts2 </tilter-name> <filter-class> org.apache.struts2.dispatcher.ng.filter.strutspreprepareandexecutefilter </filt er-class> </filter> <filter-mapping> <filter-name> struts2 </filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </eb-app>
(4) การเขียนคลาสแอ็คชั่น
แพ็คเกจ com.xmgc.sc.action; นำเข้า java.io.file; นำเข้า java.io.fileinputstream; นำเข้า java.io.fileoutputstream; นำเข้า java.io.ioException; นำเข้า org.apache.struts2.servletactionContext; คลาสสาธารณะ myuploadaction {ชื่อสตริงส่วนตัว; การอัปโหลดไฟล์ส่วนตัว; // ชื่อของประเภทไฟล์ไฟล์ในแบบฟอร์มฟอร์มเป็นสตริงส่วนตัวเดียวกัน uploadContentType; // คำนำหน้าจะต้องเป็นอัพโหลดสตริงส่วนตัวอัพโหลด uploadFileName; สตริงสาธารณะ getTitle () {return title; } โมฆะสาธารณะ settitle (ชื่อสตริง) {this.title = title; } ไฟล์สาธารณะ getUpload () {return upload; } โมฆะสาธารณะ setupload (อัปโหลดไฟล์) {this.upload = อัปโหลด; } สตริงสาธารณะ getUploadContentType () {return uploadContentType; } โมฆะสาธารณะ setuploadContentType (สตริง uploadContentType) {this.uploadContentType = uploadContentType; } สตริงสาธารณะ getUploadFileName () {return uploadfilename; } โมฆะสาธารณะ setUploadFileName (สตริง updoadFileName) {this.uploadFileName = uploadFileName; } /* สตริงสาธารณะ getSavePath () {// servletContext cxt = servletactionContext.getServletContext (); // path String = cxt.getRealPath ("/"); // เส้นทางนี้ได้รับคือ: http: // localhost: 8080/sc // หลังจากอัปโหลดแล้วมันจะกลายเป็น e: /software/apache-tomcat-6.0.45/webapps/sc return savepath; } โมฆะสาธารณะ SetSavePath (String SavePath) {//e:/software/apache-tomcat-6.0.45/webapps/sc/myupload this.savepath = servletactionContext.getServletContext () getRealPath ("/myupload"); }*/public String execute () พ่น ioexception {system.out.println (ชื่อเรื่อง); // ชื่อเรื่อง System.out.println (UploadContentType); // ประเภทไฟล์ของไฟล์ที่จะอัปโหลดระบบการโหลด (UploadFileName); String realPath = servletActionContext.getServletContext (). getRealPath ("/"); String Path = RealPath+"myupload/"+uploadfilename; System.out.println (RealPath); System.out.println (เส้นทาง); FileInputStream FIS = ใหม่ FileInputStream (อัปโหลด); fileOutputStream fos = ใหม่ fileOutputStream (พา ธ ); ไบต์ [] ไบต์ = ไบต์ใหม่ [1024]; // กำหนดอาร์เรย์ไบต์ของ 1024 ขนาด int len = -1; // ใช้เพื่อใช้เป็นบิตธงในขณะที่ ((len = fis.read (ไบต์))> 0) {fos.write (bytes, 0, len); } return null; -(5) การเขียนหน้า JSP
<%@ page contentType = "text/html; charset = utf-8"%> <!-enctype = "multipart/form-data" นี่คือการกำหนดค่าที่สำคัญที่สุด-> <form action = "myupload.action" enctype = "multipart/form-data type = "file" name = "upload"/> <br/> <input type = "submit" value = "upload"/> </form>
หลังจากสรุปนี้ฉันรู้สึกว่าการอัปโหลดไฟล์เดียวภายใต้กรอบ Struts2 ยังคงง่ายมาก เพียงแค่รับที่อยู่ที่จะเก็บไว้แล้วเขียนลงในที่อยู่นี้ผ่านสตรีมอินพุตและเอาต์พุต สำหรับงานส่วนใหญ่ Struts2 ได้ทำเพื่อเรา
ข้างต้นเป็นเนื้อหาทั้งหมดของบทความนี้ ฉันหวังว่ามันจะเป็นประโยชน์ต่อการเรียนรู้ของทุกคนและฉันหวังว่าทุกคนจะสนับสนุน wulin.com มากขึ้น