Artikel ini berbagi dengan Anda metode mengimplementasikan unggahan file dari Struts2 Framework untuk referensi Anda. Konten spesifiknya adalah sebagai berikut
Proses Konfigurasi Struts2
(1) Tambahkan paket JAR ke proyek
(2) Konfigurasi filter (filter) di web.xml
<? Xml Version = "1.0" encoding = "UTF-8"?> <Web-app Versi = "2.5" xmlns = "http://java.sun.com/xml/ns/javaee" xmlns: xsi = "http:/www.w3.org/2001 =" http:/www.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/we-noP_22 <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping></web-app>
(3) Menulis file konfigurasi struts.xml
<? Xml Version = "1.0" encoding = "UTF-8"?> <Web-App Versi = "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"> <play-name> </display-name> <filter > <tilter-name> struts2 </tilter-name> <nift-class> org.apache.struts2.dispatcher.ng.filter.strutsprepareandexecuteFilter </filter-class> </filter> <nilter-papping> <tilter-name> struts2 </filter-name> <rilter-pattern>
(4) Penulisan Kelas Aksi
paket com.xmgc.sc.action; impor java.io.file; impor java.io.fileInputStream; impor java.io.fileoutputStream; import java.io.ioException; impor org.apache.struts2.servletactionContext; kelas publik myuploadaction {private string title; Upload file pribadi; // Nama file jenis file dalam formulir formulir adalah string private yang sama unggahContentType; // awalan harus di atas unggahan string private unggahFileName; public string gettitle () {return title; } public void setTitle (string title) {this.title = title; } file publik getUpload () {return unggah; } public void setupload (unggah file) {this.upload = unggah; } public String getUploadContentType () {return unggahcontentType; } public void setuploadContentType (string unggahcontentType) {this.uploadContentType = unggahcontentType; } public string getUploadFileName () {return unggahFileName; } public void setuploadFileName (String unggahfileName) {this.uploadFileName = unggahFileName; } /* Public String getSavEpath () {// servletContext cxt = servletActionContext.getSerVletContext (); // string path = cxt.getRealPath ("/"); // Path ini adalah: http: // localhost: 8080/sc // Setelah mengunggah, itu menjadi 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 = servletActionContext.getSerVletContext (). getRealPath ("/myupload"); }*/Public String execute () melempar ioException {System.out.println (title); // title system.out.println (unggahcontentType); // jenis file dari file yang akan diunggah System.out.println (uploadFileName); // file nama file yang akan diunggah, diunggah, yang diunggah, diunggah, diunggah, diunggah, nama). String realPath = servletActionContext.getSerVletContext (). GetRealPath ("/"); String path = realpath+"myupload/"+unggahFileName; System.out.println (RealPath); System.out.println (path); FileInputStream fis = new fileInputStream (unggah); FileOutputStream fos = FileOutputStream baru (path); byte [] bytes = byte baru [1024]; // Tentukan array byte 1024 ukuran int len = -1; // digunakan untuk digunakan sebagai bit bendera sementara ((len = fis.read (bytes))> 0) {fos.write (byte, 0, len); } return null; }}(5) Menulis halaman JSP
<%@ page contentType = "Text/html; charset = utf-8"%> <!-enctype = "multipart/form-data", ini adalah konfigurasi terpenting-> <bentuk action = "myupload.action" typepe = "multipart/form-data" Metode = "post"> nama file: <input "input =": "TEKSPAR": "TEKPRIP": "POST" TEPPUT "TEKPRIP/TEKSPIT" TEKST "TEKSPOR/TEKSPIP/TEKSPET" name = "unggah"/> <br/> <input type = "kirim" value = "unggah"/> </form>
Setelah ringkasan ini, saya merasa bahwa mengunggah satu file di bawah kerangka Struts2 masih sangat sederhana. Cukup dapatkan alamat yang disimpan, dan kemudian tuliskan ke alamat ini melalui aliran input dan output. Untuk sebagian besar pekerjaan, Struts2 telah melakukannya untuk kita.
Di atas adalah semua konten artikel ini. Saya berharap ini akan membantu untuk pembelajaran semua orang dan saya harap semua orang akan lebih mendukung wulin.com.