In upload. on the jsp page, multiple file domain objects are named with the same name. In this way, multiple file domains can be parsed into an array in action. The size of the array is the number of file domains, at the same time, a file domain is parsed into three corresponding variables. Therefore, multiple file domains correspond to three arrays. The size of each array is the number of file domains. The jsp page code is as follows:
Copy codeThe 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 file: <input type = "file" name = "upload"/> <br/>
Select file 2: <input type = "file" name = "upload"/> <br/>
Select file 3: <input type = "file" name = "upload"/> <br/>
<Input type = "submit" value = "upload"/>
</Form>
The corresponding actions traverse all file fields in sequence, and then generate the corresponding input file stream. The output file stream adds the corresponding output file stream to the specified server storage path to save the file. At the same time, the storage path of files on the server is dynamically specified.
The action Code is as follows:
Copy codeThe 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 FCM = new FileInputStream (files [I]);
Int len = 0;
While (len = FS. read (buffer)> 0 ){
Fos. write (buffer, 0, len );
}
}
Return SUCCESS;
}
}
The struts. xml file is configured as follows: configure the File Upload interceptor, the allowed file types, and the size of the file to be uploaded. At the same time, introduce the defaultStack interceptor and the storage location of the uploaded files on the server.
Copy codeThe Code is 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>
Web. the xml file code is as follows: the struts-cleanup filter is configured, which has no direct impact on the file upload function. However, as a helper class of the struts core filter, the system is more stable and unknown exceptions are eliminated.Copy codeThe Code is 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>
All uploaded images are displayed on the upload success page:
The success. jsp page code is as follows:
Copy codeThe Code is as follows: file title: <s: property value = "title"/> <br/>
First image: <br/>
Figure 2: <br/>
Strus2 also supports using list to upload multiple files at the same time. Its principle is the same as that of array, and there is no fundamental difference. The only difference is to change all arrays to list. In addition, you can use list to encapsulate file domain parameters by modifying the access method of list. Allows simultaneous upload of multiple files.