Struts file upload (multiple files)

Source: Internet
Author: User

No. 01 Step: Configure Web. xml

<?xml version= "1.0" encoding= "UTF-8"? ><web-app version= "2.4"xmlns= "HTTP://JAVA.SUN.COM/XML/NS/J2EE"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "Http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd "><filter> <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-pat Tern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.jsp</we Lcome-file> </welcome-file-list></web-app>

No. 02 Step: Writing The Action class

 Packagecom.self.action;ImportJava.io.File;Importorg.apache.commons.io.FileUtils;ImportOrg.apache.struts2.ServletActionContext;ImportCom.opensymphony.xwork2.ActionContext;/**File Upload **/ Public classFileUpload {Privatefile[] Image;//get the uploaded file    PrivateString[] Imagefilename;//gets the name of the uploaded file, naming the rule: page property name +filename    PrivateString[] Imagecontenttype;//get the type of upload file         PublicFileUpload () {}/**Upload Method **/     PublicString UploadFile () {Try{String Realpath=servletactioncontext.getservletcontext (). Getrealpath ("/images"); System.out.println ("Project Path (/images path, File save path):" +Realpath); if(image!=NULL) {System.out.println ("Already got upload file!" "); File Parentfile=NewFile (Realpath);//Specify file Save path            if(!parentfile.exists ()) {//save path does not exist, createParentfile.mkdirs (); }             for(inti=0;i<image.length;i++) {File saveFile=NewFile (Parentfile,imagefilename[i]);//parentfile: Save path, Imagefilename: Save file nameFileutils.copyfile (Image[i], saveFile);//Copy the uploaded image to SaveFile} actioncontext.getcontext (). Put ("Message", "Saved successfully!" "); }        } Catch(Exception e) {e.printstacktrace (); }        return"Tsuccess"; }     Publicfile[] GetImage () {returnimage; }     Public voidsetimage (file[] image) { This. Image =image; }     Publicstring[] Getimagefilename () {returnImagefilename; }     Public voidsetimagefilename (string[] imagefilename) { This. Imagefilename =Imagefilename; }     Publicstring[] Getimagecontenttype () {returnImagecontenttype; }     Public voidSetimagecontenttype (string[] imagecontenttype) { This. Imagecontenttype =Imagecontenttype; }}

No. 03 Step: Configure Struts.xml

<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE struts    Public " -//apache software foundation//dtd Struts Configuration 2.0//en "    http://struts.apache.org/dtds/struts-2.0.dtd" ><struts>    <constant name= "struts.action.extension" value= "do,action"/>        <package  extends= "Struts-default" >        class= "Com.self.action.FileUpload" method= "{1}" >            <result name= "tsuccess" >                /outdata.jsp            </result>        </action>    </Package ></struts>

No. 04 Step: Writing The interface

Import= "java.util.*" pageencoding= "Utf-8"%>          file 1:<input type= "file" Name= " Image >           file 2:<input type= "file" name= "image" >           Files 3:<input type= "file" Name= " Image >          <BR>          <input type= "Submit" value= "Upload file" >      </form>  </body> 

No. 05 Step: Precautions

Please refer to the following: Struts file upload (single file)

No. 06 Step: Import the Package

Struts file upload (multiple files)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.