In struts2 multiple files are uploaded simultaneously

  • 2020-04-01 01:38:14
  • OfStack

In the upload. The JSP page will be multiple files in a domain object named the same name, this can be multiple files in the action field parsed into an array, the array size is the file number of fields, a file at the same time domain parsed into three corresponding variables, so multiple files domain corresponding to three arrays, each array size is the number of file realm. The JSP page code is as follows:


<form action="upload.action" name="uploadForm" method="post" enctype="multipart/form-data">
 File title: <input type="text" name="title"/><br/>
 Select the file - : <input type="file" name="upload"/><br/>
 Select document 2: <input type="file" name="upload"/><br/>
 Select document 3: <input type="file" name="upload"/><br/>
<input type="submit" value="upload"/>
</form>

The corresponding Action traverses all file fields in turn, 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 specify the save path for the file on the server.

The action code is 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;
}
}

Struts.xml file is configured as follows: configure the interceptor for file upload, the type of file allowed to be uploaded, the upload file size limit, and introduce the defaultStack interceptor and the save location of the upload file on the server


<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 code of the web.xml file is as follows: struts-cleanup filter is configured, which has no direct impact on the upload function of the file, but the auxiliary class as the struts core filter makes the system more stable and eliminates unknown exceptions.

<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 on the upload success interface:

The code for the success.jsp page is as follows:


 File title: <s:property value="title"/><br/>
 First picture: <img alt="first" src="<s:property value="'upload/'+uploadFileName[0]"/>"/><br/>
 Second picture: <img alt="second" src="<s:property value="'upload/'+uploadFileName[1]"/>"/><br/>

Strus2 also supports the use of list to upload multiple files at the same time. It just changes the array to a list. And modify the access method of the list to use the list to encapsulate the file domain parameters. Multiple files can be uploaded simultaneously.


Related articles: