Struts2 to implement multiple files at the same time upload code _java

Source: Internet
Author: User
Tags constant i18n

Multiple file domain objects are named the same name in the Upload.jsp page. This allows multiple file fields to be parsed into an array in the action, and the size of the array is the number of file fields, and a file field resolves to three corresponding variables, so multiple file fields correspond to three arrays, each of which is the size of the file field. 。 The JSP page code is as follows:

Copy Code code as follows:

<form action= "upload.action" name= "Uploadform" method= "post" enctype= "Multipart/form-data" >
File title: <input type= "text" name= "title"/><br/>
Select File-:<input type= "file" name= "upload"/><br/>
Select File Two: <input type= "file" name= "upload"/><br/>
Select File Three: <input type= "file" name= "upload"/><br/>
<input type= "Submit" value= "Upload"/>

</form>

The corresponding action iterates through all the file fields, then generates the corresponding input file stream, and the output file stream adds the corresponding output file stream to the specified server save path to save the file. Also dynamically specifies the save path for the file on the server.

The action code is as follows:

Copy Code code as follows:

Package com.inspur.action;

Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;

Import Org.apache.struts2.ServletActionContext;

Import Com.opensymphony.xwork2.ActionSupport;

public class Uploadaction extends Actionsupport {
Private String title;
Private file[] upload;
Private string[] Uploadfilename;
Private string[] Uploadcontenttype;
Private String Savepath;

Public String GetTitle () {
return title;
}
public void Settitle (String title) {
This.title = title;
}
Public file[] Getupload () {
return upload;
}
public void Setupload (file[] upload) {
This.upload = upload;
}
Public string[] Getuploadfilename () {
return uploadfilename;
}
public void Setuploadfilename (string[] uploadfilename) {
This.uploadfilename = Uploadfilename;
}
Public string[] Getuploadcontenttype () {
return uploadcontenttype;
}
public void Setuploadcontenttype (string[] uploadcontenttype) {
This.uploadcontenttype = Uploadcontenttype;
}
Public String Getsavepath () {
Return Servletactioncontext.getrequest (). Getrealpath (Savepath);
}
public void Setsavepath (String savepath) {
This.savepath = Savepath;
}
Public String upload () throws exception{
File[] Files=this.getupload ();
for (int i=0;i<files.length;i++) {
FileOutputStream fos=new FileOutputStream (This.getsavepath () + "\" +this.getuploadfilename () [i]);
Byte[] Buffer=new byte[1024];
FileInputStream fis=new FileInputStream (Files[i]);
int len=0;
while ((Len=fis.read (buffer)) >0) {
Fos.write (Buffer,0,len);
}
}


return SUCCESS;
}

}

The Struts.xml file configuration is as follows: The configuration file uploads the interceptor, allows the upload file type, uploads the file size limit, simultaneously introduces the Defaultstack interceptor and uploads the file to save the location on the server

Copy Code code as follows:

<struts>
<constant name= "struts.custom.i18n.resources" value= "message" ></constant>
<constant name= "struts.i18n.encoding" value= "GBK" ></constant>
<package name= "Uploadmult" extends= "Struts-default" namespace= "/" >
<action name= "Upload" class= "com.inspur.action.UploadAction" method= "Upload" >
<interceptor-ref name= "FileUpload" >
<param name= "Allowedtypes" >image/bmp,image/png,image/gif,image/jpeg</param>
<param name= "MaximumSize" >20000000000</param>
</interceptor-ref>
<interceptor-ref name= "Defaultstack" ></interceptor-ref>
<param name= "Savepath" >/upload</param>
<result name= "Success" >/success.jsp</result>
<result name= "Input" >/upload.jsp</result>
<result name= "Error" >/error.jsp</result>
</action>
</package>

</struts>


The Web.xml file code is as follows: Configure the Struts-cleanup filter, the file upload function has no direct impact, but as the Struts Core filter auxiliary class is the system more stable, eliminate unknown exceptions.
Copy Code code as follows:

<filter>
<filter-name>struts-cleanup</filter-name>
<filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>
</filter>
<filter-mapping>
<filter-name>struts-cleanup</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

Display all uploaded pictures in the upload success interface:

The success.jsp page code is as follows:

Copy Code code as follows:

File title: <s:property value= "title"/><br/>
First Picture: <br/>
Second Picture: <br/>

STRUS2 also supports the use of the list to upload multiple files at the same time, the principle and array is the same, there is no fundamental difference. is simply to change the array to list. You can use list to encapsulate file field parameters by modifying the list access method. Implement simultaneous uploads of 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.