STRUTS2 multi-File upload principles and examples

Source: Internet
Author: User
Tags create directory

First, create the page to upload the file, the code is as follows

1. Struts2 can also be easily implemented with multiple file uploads. Add more than one file field to the Input form field: multifileupload.jsp

1 <%@ Page Language="Java"ContentType="text/html; Charset=utf-8"%>2 <%@ taglib URI="/struts-tags"prefix="s" %>3 <HTML>4 <Head>5     <title>Multiple file uploads</title>6 </Head>7 <Body>8     <FontColor= "Red"><S:fielderror/></Font>9     <formAction= "Multifileupload.action"Method= "POST"enctype= "Multipart/form-data">TenFile title:<inputtype= "text"name= "title"size= " the"value= "${param.title}"/><BR/> One        <!--set two file fields with the same name - ASelect the first file:<inputtype= "File"name= "Upload"size= " the"/><BR/> -Select a second file:<inputtype= "File"name= "Upload"size= " the"/><BR/> -        <inputtype= "Submit"value= "Upload"/>        the     </form> - </Body> - </HTML>

2. Encapsulate the multiple file domains in an action class using an array: Multifileuploadaction.java      

1  Packageorg.qiujy.web.struts2;2 ImportJava.io.BufferedInputStream;3 ImportJava.io.BufferedOutputStream;4 ImportJava.io.File;5 ImportJava.io.FileInputStream;6 ImportJava.io.FileOutputStream;7 Importjava.io.IOException;8 ImportJava.io.InputStream;9 ImportJava.io.OutputStream;Ten ImportOrg.apache.struts2.ServletActionContext; One ImportCom.opensymphony.xwork2.ActionSupport; APublicclass multifileuploadactionextendsActionsupport { -Privatestaticfinalintbuffer_size = 16 * 1024; -     //file Title the     PrivateString title; -     //to encapsulate multiple uploaded file domain objects with file arrays -     Privatefile[] upload; -     //to encapsulate multiple upload file names with a string array +     Privatestring[] Uploadfilename; -     //to encapsulate multiple upload file types with a string array +     Privatestring[] Uploadcontenttype; A     //directory path to save files (via Dependency injection) at     PrivateString Savepath; -     //The following are getter and setter for all attributes. Omitted...  -     //one of your own packages copy the source file object into the target file object - privatestaticvoid Copy (file src, file dst) { -InputStream in =NULL; -OutputStream out =NULL; in         Try { -in =NewBufferedinputstream (NewFileInputStream (SRC), buffer_size); toout =NewBufferedoutputstream (NewFileOutputStream (DST), + buffer_size); -             byte[] buffer =Newbyte[buffer_size]; the             intLen = 0; *              while(len = in.read (buffer)) > 0) { $Out.write (buffer, 0, Len);Panax Notoginseng             } -}Catch(Exception e) { the e.printstacktrace (); +}finally { A             if(NULL!=In ) { the                 Try { + in.close (); -}Catch(IOException e) { $ e.printstacktrace (); $                 } -             } -             if(NULL!=Out ) { the                 Try { - out.close ();Wuyi}Catch(IOException e) { the e.printstacktrace (); -                 } Wu             } -         } About     } $ @Override -      PublicString Execute ()throwsException { -file[] Srcfiles = This. Getupload (); -         //process Each file that you want to upload A          for(inti = 0; i < srcfiles.length; i++) { +             //Create directory file full path based on server's file save address and original file name theString Dstpath =Servletactioncontext.getservletcontext () -. Getrealpath ( This. Getsavepath ()) $+ "\" + this.getuploadfilename () [i]; theFile Dstfile =NewFile (dstpath); the              This. Copy (Srcfiles[i], dstfile); the         } the returnsuccess; -     } in}

3, in Struts.xml configuration only need to change the name of the action to configure the rest of the basic do not need to change what

Struts2 Multi-File upload principles and examples

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.