1024programmer Blog Struts2 file upload (multiple file upload)

Struts2 file upload (multiple file upload)

class=”htmledit_views”>

Look at the effect picture first:

Input Interface

result page

Enter interface code:


 
 
 
 
 
 
 Insert title here
 
 
 

Multiple file upload

File title:
Select file 1:
Select file 2:
Select file 3:

action part code:

package com.hcj.action;

 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.util.List;

 import org.apache.struts2.ServletActionContext;

 import com.opensymphony.xwork2.ActionSupport;

 public class UploadManyAction extends ActionSupport {
 //Encapsulate the attributes of the file header request parameter
 private String title;
 //Encapsulate the attributes of the uploaded file domain (the attribute name is the same as the name value in the front-end input)
 private List upload;
 //Encapsulate the attribute of the uploaded file type (the attribute name is the foreground name value +ContentType, if the foreground name value here is upload, so here is uploadContentType)
 private List uploadContentType;
 //Encapsulate the attribute of the uploaded file name (the attribute name is the foreground name value + FileName, if the foreground name value here is upload, so here is uploadFileName)
 private List uploadFileName;
 //Configure the save path (configure directly in the struts.xml file)
 private String savePath;
 	
 public String getTitle() {
 return title;
 }

 public void setTitle(String title) {
 this.title = title;
 }

 public List getUpload() {
 return upload;
 }

 public void setUpload(List upload) {
 this.upload = upload;
 }

 public List getUploadContentType() {
 return uploadContentType;
 }

 public void setUploadContentType(List uploadContentType) {
 this.uploadContentType = uploadContentType;
 }

 public List getUploadFileName() {
 return uploadFileName;
 }

 public void setUploadFileName(List uploadFileName) {
 this.uploadFileName = uploadFileName;
 }

 public String getSavePath() {
 /**
 		 * Precautions
 * 1. It is not the same as other methods, simply return savePath
 * 2. Some books may use ServletActionContext.getRequset().getRealPath(savePath) to return the path to the server here.
 * But this method has expired, now use ServletActionContext.getServletContext().getRealPath to replace this method
 * 3. If you report The method getServletContext() from the type ServletActionContext refers to the missing type ServletContext error in the second step,
 * It is because the servlet-api.jar package is missing, which can be found in the lib file in your tomcat directory, just import it into the lib package in your WEB-INF.
 */
 return ServletActionContext.getServletContext().getRealPath(savePath);
 		 
 }

 public void setSavePath(String savePath) {
 this. savePath = savePath;
 }
 	

 public String upload() throws Exception {
 // Get the array of files that need to be uploaded
 List files = getUpload();
 //Loop through each file that needs to be uploaded
 for (int i = 0; i 0){
 //write target file
 fos.write(buffer, 0, len);
 }
 //�flow
 fos. close();
 fis. close();
 }
 return SUCCESS;
 }
 }
 

struts.xml part code:


 
 
 
 
 
 
 
 
 
 
 
 
 
 
 /success
 
 		
 		
 
 
 
 
 
 
 image/bmp, image/png, image/gif, image/jpeg, text/plain, application/vnd.ms-excel, application/vnd.openxmlformats-officedocument.wordprocessingml.document
 
 <!-- Configure the file size allowed to be uploaded to 2M, the default size of struts2 file upload is 2M,
 If you need to upload a file exceeding 2M, you need to add such a sentence in the struts.xml configuration file  -->
 2097152
 
 
 
 
 /upload
 
 /suc2.jsp
 /error.jsp
 /uploadinput.jsp
 
 
 

Response result interface:


 
 <base href="">
 
 
 
 
 
 
 Uploaded successfully
 
 
 
 
 File title:


The file is: image uploaded

<img src=""/>

Attached: struts2 file upload (single file upload) part:
http://blog.csdn.net/u011768325/article/details /45362687

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/struts2-file-upload-multiple-file-upload/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

The latest and most comprehensive programming knowledge, all in 1024programmer.com

© 2023 1024programmer - Encyclopedia of Programming Field
Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us