Dieser Artikel teilt mit Ihnen die Methode zum Implementieren von Datei -Upload von Struts2 -Framework als Referenz. Der spezifische Inhalt ist wie folgt
Struts2 -Konfigurationsprozess
(1) Fügen Sie dem Projekt JAR -Paket hinzu
(2) Konfiguration des Filters (Filter) in 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-App-App- <Filter-name> Struts2 </filter-name> <filterklasse> org.apache.struts2.dispatcher.ng.filter.strutsprepareAndexecuteFilter </Filterklasse> </Filter> <Filter-Mapping> <Filter-Stuts2 </filter-name> <urlpattern> </filter-name> <urlpattern> </filter-name> <urlpattern>/*</filter-name> <urlpattern>/*</aveNname </filter-mapping> </web-App>
(3) Schreiben von Struts.xml Konfigurationsdatei
<xml Version = "1.0" coding = "utf-8"?> <web-App Version = "2.5" xmlns = "http://java.sun.com/xml/ns/javaee" xmlns: xsi = "http://www.w3.org/2001/xmlschema-instance" xsi: Schemalocati auf = "http://java.sun.com/xml/ns/javaeHttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <display-name> </display-name> <filter > <Filter-name> Struts2 </Filter-Name> <filter-Klasse> org.apache.struts2.Dispatcher.ng.filter.strutsprepareAndexecuteFilter </filt ER-CLASS> </filter> <Filter-Mapping> <Filter-name> Struts2 </filter-name> <URL-Muster>/*</url-puftern> </filter-mapping> </web-App>
(4) Schreiben des Aktionsklassens
Paket com.xmgc.sc.action; import Java.io.file; import Java.io.fileinputStream; Import Java.io.FileOutputStream; Import Java.io.ioxception; import org.apache.struts2.servletactioncontext; öffentliche Klasse MyUploadAction {private String -Titel; private Datei hochladen; // Der Name des Dateityps im Formularformular ist dieselbe private String -UploadContentType; // Das Präfix muss der oben genannte hochladen private String -UploadFileName sein. public String gettitle () {return title; } public void settitle (String title) {this.title = title; } öffentliche Datei getUpload () {return upload; } public void setUpload (Datei -Upload) {this.Upload = upload; } public String GetUploadContentType () {return uploadContentType; } public void setUploadContentType (String uploadContentType) {this.UploadContentType = UploadContentType; } public String getUploadFileName () {return uploadFileName; } public void setUploadFileName (String uploadFileName) {this.UploadFileName = UploadFileName; } /* public String getavePath () {// servletContext cxt = servletActaceContext.getServletContext (); // String path = cxt.getrealPath ("/"); // Dieser Pfad erhält: http: // localhost: 8080/sc // Nach dem Hochladen wird es zu 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 = servletActaceContext.getServletContext (). getRealPath ("/myuPload"); }*/public String execute () löscht ioException {System.out.println (title); // title system.out.println (UploadContentType); // Dateityp der Datei, die hochgeladen wird. String realPath = ServletActionContext.getServletContext (). GetRealPath ("/"); String path = RealPath+"MyUpload/"+UploadFileName; System.out.println (RealPath); System.out.println (Path); FileInputStream fis = new FileInputStream (Upload); FileOutputStream fos = new FileOutputStream (Pfad); byte [] bytes = new Byte [1024]; // Definieren Sie ein Byte-Array von 1024 Größe int len = -1; // werden als Flag-Bits verwendet ((len = fis.read (bytes))> 0) {fos.write (bytes, 0, len); } return null; }}(5) Schreiben von JSP -Seiten
<%@ page contentType = "text/html; charSet = utf-8"%> <!-engType = "MultiPart/Form-Data", dies ist die wichtigste Konfiguration-> <form action = "myUpload.Action" EncTTYPE = "multiPart/Form-Data" method ". type = "Datei" name = "hochladen"/> <br/> <Eingabe type = "value =" upload "/> </form>
Nach dieser Zusammenfassung bin ich der Meinung, dass das Hochladen einer einzelnen Datei im Rahmen des Struts2 -Frameworks immer noch sehr einfach ist. Lassen Sie einfach die Adresse gespeichert und schreiben Sie sie dann über den Eingangs- und Ausgabestream an diese Adresse. Während des größten Teils der Arbeit hat Struts2 es für uns getan.
Das obige ist der gesamte Inhalt dieses Artikels. Ich hoffe, es wird für das Lernen aller hilfreich sein und ich hoffe, jeder wird Wulin.com mehr unterstützen.