Cet article partage avec vous la méthode d'implémentation de téléchargement de fichiers de Struts2 Framework pour votre référence. Le contenu spécifique est le suivant
Processus de configuration de Struts2
(1) Ajouter un package JAR au projet
(2) Configuration du filtre (filtre) dans web.xml
<? xml version = "1.0" encoding = "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: 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_5.xsd"> <splay-name> <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-sattern> </filter-mapping> </ web-app>
(3) Rédaction du fichier de configuration de struts.xml
<? xml version = "1.0" Encoding = "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 on = "http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <plaw-name> </ display-name> <filter > <filter-name> struts2 </ filter-name> <filter-class> org.apache.struts2.dispatcher.ng.filter.strutsprepareandexeccutefilter </ filt Er-Class> </ Filter> <Imlter-Mapping> <Imter-Name> Struts2 </ Filter-Name> <Url-Pattern> / * </url-Pattern> </ Filter-Mapping> </low-Apple>
(4) Écriture de classe d'action
package com.xmgc.sc.action; import java.io.file; import java.io.fileInputStream; import java.io.fileoutputStream; import java.io.ioException; import org.apache.struts2.ServletActionContext; classe publique MyuPloadAction {Titre de la chaîne privée; Téléchargement de fichiers privé; // Le nom du type de fichier Type dans le formulaire du formulaire est la même chaîne privée uploadContentType; // Le préfixe doit être le téléchargement ci-dessus de la chaîne privée UploadFileName; public String getTitle () {return title; } public void SettItle (String Title) {this.title = title; } public File getUpload () {return upload; } public void setupload (fichier 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 getSavePath () {// ServletContext cxt = servletActionContext.getServletContext (); // String path = cxt.getRealPath ("/"); // Ce chemin obtient est: http: // localhost: 8080 / sc // Après le téléchargement, il devient 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 () lève ioException {System.out.println (title); // title system.out.println (uploadContentType); // type de fichier du fichier à télécharger system.out.println (uploadfilename); // nom de fichier du fichier à télécharger, rappelez-vous qu'il y a aussi le système d'extension.out.out.Trintln (le fichier); String realPath = servletActionContext.getServletContext (). GetRealPath ("/"); String path = realPath + "myupload /" + uploadFileName; System.out.println (realPath); System.out.println (chemin); FileInputStream fis = new FileInputStream (upload); FileOutputStream fos = new FileOutputStream (path); octet [] octets = nouveau octet [1024]; // définir un tableau d'octet de 1024 taille int len = -1; // utiliser pour être utilisé comme bits de drapeau while ((len = fis.read (bytes))> 0) {fos.write (bytes, 0, len); } return null; }}(5) Écriture de pages JSP
<% @ page contenttype = "text / html; charset = utf-8"%> <! - EncType = "multipart / form-data", c'est la configuration la plus importante -> <form action = "myupload.action" enctype = "multipart / form-data" method = "post"> File name: <entre type = "text" name = "title" /> <br/> up: <puty type = "text" name = "title" name = "upload" /> <br/> <input type = "soumi" value = "upload" /> </ form>
Après ce résumé, je pense que le téléchargement d'un seul fichier sous le cadre Struts2 est toujours très simple. Il suffit de stocker l'adresse, puis d'écrire à cette adresse via le flux d'entrée et de sortie. Pour la plupart du travail, Struts2 l'a fait pour nous.
Ce qui précède est tout le contenu de cet article. J'espère que cela sera utile à l'apprentissage de tous et j'espère que tout le monde soutiendra davantage Wulin.com.