1. Ideen zum Hochladen von Struts -Dateien
Ich habe auch zuvor über die Datei -Pload -Komponente gesprochen. Es hat leistungsstarke Funktionen, aber der Betrieb ist kompliziert und kompliziert. Dieses Mal werden auch Datei -Uploads in Strust unterstützt und FileUpload wird verpackt, was viel bequemer zu verwenden ist.
Hier kommt ein Tag und eine Klasse:
<HTML: Datei Eigenschaft = "entspricht dem Eigenschaftsnamen in ActionForm">
Verwenden Sie dieses Tag, um Dateien hochzuladen. Um den Inhalt zu empfangen
Die Schnittstelle ist abgeschlossen.
2. Realisierung
Sh.jsp:
<%@ page Language = "java" import = "java.util. URI = "http://jakarta.apache.org/struts/tags-html" prefix = "html"%> <%@ taglib uri = "http://jakarta.apache.org/struts/Tags-logic prefix =" logic "igic"%> <! Html 4.01 transsitional // en "> <html> <kopf> <titels> sh.jsp </title> <meta http-äquiv =" content-type "content =" text/html; EncTTEpe = "MultiPart/Form-Data"> <HTML: Dateieigenschaft = "Foto"> </html: file> <html: value = "upload"> </html: sure> </html: Formular> </body> </html>> </html> </html>
Uploadform.java:
paket com.zyy.struts.form; import org.apache.struts.action.actionForm; import org.apache.struts.upload.formalfile; öffentliche Klasse UploadForm erweitert ActionForm {private formfile foto; public formFile getphoto () {return photo; } public void setPhoto (FormFile photo) {this.photo = foto; }} Iptimestamp.java:
Paket com.zyy.util; import Java.text.SimpledateFormat; Import Java.util.date; Import Java.util.Random; öffentliche Klasse Iptimestamp {private simpleDateFormat sdf = null; private String ip = null; public iptimestamp () {} public iptimestamp (String ip) {this.ip = ip; } public String getiptimerand () {stringBuffer bufer = new StringBuffer (); if (this.ip! = null) {string s [] = this.ip.Split ("//."); für (int i = 0; i <sength; i ++) {buf.append (this.addzero (s [i], 3)); }} buf.append (this.gettimestamp ()); Random r = neu random (); für (int i = 0; i <3; i ++) {buf.append (r.Nextint (10)); } return buf.toString (); } public String getDate () {this.sdf = new SimpledateFormat ("yyyy-mm-dd hh: mm: ss.sss"); return this.sdf.format (neues Datum ()); } public String getTimestamp () {this.sdf = new SimpledateFormat ("yyyymmmdhhmmssssss"); return this.sdf.format (neues Datum ()); } private String addzero (String str, int len) {stringBuffer s = new StringBuffer (); S.Append (str); while (s.Length () <len) {S.Insert (0, "0"); } return s.ToString (); } public static void main (String args []) {System.out.println (neuer iptimestamp ("192.168.1.1"). Getiptimerand ()); }}Uploadaction.java:
Paket com.zyy.struts.action; import java.io.file; import Java.io.fileOutputStream; Import Java.io.inputStream; Import Java.io.outputStream; Import Javax.Servlet.http.httpletReque; org.apache.struts.action.action; import org.apache.struts.action.actionForm; import org.apache.structs.action.actionforward; import org.apache.structs.action.ActionMapping; importieren com.zyy.structs.form.Upload. Formular, httpServletRequest -Anforderung, httpServletResponse -Antwort) löst eine Ausnahme aus {Uploadform UploadForm = (UploadForm) Formular; Iptimestamp ips = new iptimestamp (request.getRemoteaddr ()); // Dateiname String Dateiname = ips.getiptimerand () + "." + UploadForm.getPhoto (). getFileName (). Split ("//.") [UploadForm.getPhoto (). getFileName (). Split ("//."). Länge - 1]; // Ausgabe Pfaddatei outfile = neue Datei (super.getServlet (). GetServletContext () .getRealPath ("/") + "Upload" + Datei.Separator + Dateiname); // Der Ordner, in dem das Bild gespeichert ist, filedatei = neue Datei (Super.getServlet (). GetServletContext () .GetRealPath ("/") + "Upload"); if (! file.exists ()) {file.mkdir (); } InputStream input = uploadForm.getPhoto (). GetInputStream (); OutputStream output = new FileOutputStream (Outfile); Byte -Daten [] = neues Byte [1024]; int temp = 0; while ((temp = input.read (Daten, 0, 1024))! = -1) {output.write (Daten); } output.close (); input.close (); null zurückkehren; }}Struts-config.xml:
<? <form-beans> <form-bean name="uploadForm" type="com.zyy.struts.form.UploadForm"></form-bean> </form-beans> <global-exceptions> </global-exceptions> <global-forwards> </global-forwards> <action-mappings> <action path="/jsp/upload" attribute="uploadForm" input="/jsp/sh.jsp" name = "uploadForm" scope = "request" type = "com.zyy.struts.action.uploadaction"> </action> </action-mappings> <message-resources parameter = "ressource.messageresources"/> </struts-config>
Da ich speichere, ist super.getServlet (). GetServletContext (). GetRealPath ("/")+ "Upload"
Dies ist der eigentliche Weg des virtuellen Verzeichnisses im Upload -Ordner.
Es ist ersichtlich, dass das Prinzip des Datei -Uploads in Struts mit FileUploads entspricht, aber Struts verpackt ist. Daher ist es offensichtlich viel bequemer als die Verwendung der FileUpload -Komponente allein zu verwenden.
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.