Use of the Java Web struts2 framework-multiple file uploads based on forms

Source: Internet
Author: User
Tags java web

and a single file upload configuration is the same, just accept the parameter in the action, accept the array, is no longer a single file.

One, the action implementation:

public class Mutablefilesupload extends Actionsupport {private static final long Serialversionuid = 1l;public file[] Uploa Dobjects;public string[] uploadobjectsfilename;public string[] uploadobjectscontenttype;public String moreInfo; Public String upload () {System.out.println (moreinfo);//1. Get Servletcontextservletcontext ServletContext = Servletactioncontext.getservletcontext ();//2. Call the Realpath method to get the real directory based on a virtual directory string Realpath = Servletcontext.getrealpath ("/files");//3. If the real directory does not exist, you need to create a file File = new file (Realpath); if (!file.exists ()) { File.mkdirs ();} 4. Save the File//cut: Cut the temporary file to the specified location and rename it. Note: The temp file does not have a for (int i = 0; i < uploadobjects.length; i++) {System.out.println ("one file name:" + uploadobjectsfilename[ I]); Uploadobjects[i].renameto (new file (file, uploadobjectsfilename[i]));} return SUCCESS;}}

  

Two, configure the action

<action name= "Mutable_uploads" class= "gy.upload.MutableFilesUpload" method= "Upload" > <!--Specify (limit) the type of upload file, Define the local interceptor, modify the default Interceptor's property "Fileupload.maximumsize": Limit the upload maximum file size. "Fileupload.allowedtypes": the type of file that is allowed to be uploaded. "Fileupload.allowedextensions": An extensible file type that allows files to be uploaded. --<interceptor-ref name= "Defaultstack" > <param name= "fileupload.maximumsize" >500000000</param > <param name= "fileupload.allowedtypes" >text/plain,application/vnd.ms-powerpoint</param> <param Name= "Fileupload.allowedextensions" >.txt,.ppt</param> </interceptor-ref> <result>/ Success.html</result> <!--An error automatically returns the input result, into the results view--<result name= "input" >/fail.html</result ></action>

Third, the implementation of the form

<body><form action= "Mutable_uploads" method= "post" enctype= "Multipart/form-data" > File:<br> < Input type= "file" Name= "uploadobjects" ><br><input type= "file" Name= "Uploadobjects" ><br> more Info:<input type= "text" name= "moreinfo" ><br><input type= "Submit" value= "Submit" ><br></form ></body>

  

  

Use of the Java Web struts2 framework-multiple file uploads based on forms

Related Article

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.