Preface
Learn how to upload and download files in the Struts 2 framework.
File upload
Struts 2 provides support for commons-fileupload components. Uploading files requires importing two Jar packages or (Maven dependencies)
- common-fileuplaod-x.x.x.jar
- commons-io-x.x.x.jar
Single file upload
1. Prepare an upload page (Jsp)
- The form method must be post;
- Entype must be set and the value of enctype must be multipart/form-data.
<s:form action="upload" enctype="multipart/form-data" method="POST"> //Select files: <s:file name="upload" label="Select Files"/> <br> <s:submit name="sub" value="File upload"/> <br> </s:form>
2. Action for single file upload
package com.bdqn.Control; import com.opensymphony.xwork2.ActionSupport; import org.apache.struts2.ServletActionContext; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; public class UploadAction extends ActionSupport { //Encapsulate the file object of the upload server private File upload; //Types of uploaded files private String uploadContentType; //Name of uploaded file private String uploadFileName; //Get the save path of the uploaded file private String savePath; public File getUpload() { return upload; } public void setUpload(File upload) { this.upload = upload; } public String getUploadContentType() { return uploadContentType; } public void setUploadContentType(String uploadContentType) { this.uploadContentType = uploadContentType; } public String getUploadFileName() { return uploadFileName; } public void setUploadFileName(String uploadFileName) { this.uploadFileName = uploadFileName; } public String getSavePath() { return ServletActionContext.getServletContext().getRealPath(savePath); } public void setSavePath(String savePath) { this.savePath = savePath; } @Override public String execute() throws Exception { byte[] bytes = new byte[1024]; //read file FileInputStream fileInputStream = new FileInputStream(getUpload()); //Save the file and set the save path System.out.println(getSavePath()+"-------------->"+getUploadFileName()); FileOutputStream fileOutputStream = new FileOutputStream(getSavePath()+"\\"+getUploadFileName()); int length=fileInputStream.read(bytes); while (length>0){ //Write the contents of length each time fileOutputStream.write(bytes,0,length); length=fileInputStream.read(bytes); } fileInputStream.close(); fileOutputStream.flush(); fileOutputStream.close(); return SUCCESS; } }
3. Configure struts.xml file
<!--File upload--> <action name="upload" class="com.bdqn.Control.UploadAction"> <!--Set the path to save the directory--> <param name="savePath">/uploads</param> <result name="success">ZhanUpload.jsp</result> </action>
Note: ServletActionContext.getServletContext().getRealPath(savePath) in Action; red, import Servlet's Api dependency (jar package)
<dependency> <groupId>javax</groupId> <artifactId>javaee-api</artifactId> <version>7.0</version> </dependency>
Multi-file upload
The operation of multi-file upload is very simple, adding multiple File controls with the same name attribute to the form, so that when the form is submitted, an array will be submitted.
<s:form action="upload" enctype="multipart/form-data" method="POST"> //Select files: <s:file name="upload" label="Select Files"/> <br> <s:file name="upload" label="Select Files"/> <br> <s:submit name="sub" value="File upload"/> <br> </s:form>
package com.bdqn.Control; import com.opensymphony.xwork2.ActionSupport; import org.apache.struts2.ServletActionContext; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; public class DuoUploadAction extends ActionSupport { //Encapsulate the file object of the upload server private File[] upload; //Types of uploaded files private String[] uploadContentType; //Name of uploaded file private String[] uploadFileName; //Get the save path of the uploaded file private String savePath; public File[] getUpload() { return upload; } public void setUpload(File[] upload) { this.upload = upload; } public String[] getUploadContentType() { return uploadContentType; } public void setUploadContentType(String[] uploadContentType) { this.uploadContentType = uploadContentType; } public String[] getUploadFileName() { return uploadFileName; } public void setUploadFileName(String[] uploadFileName) { this.uploadFileName = uploadFileName; } public String getSavePath() { return ServletActionContext.getServletContext().getRealPath(savePath); } public void setSavePath(String savePath) { this.savePath = savePath; } @Override public String execute() throws Exception { byte[] bytes = new byte[1024]; for (int i = 0; i <upload.length ; i++) { //read file FileInputStream fileInputStream = new FileInputStream(getUpload()[i]); //Save the file and set the save path FileOutputStream fileOutputStream = new FileOutputStream(getSavePath()+"\\"+getUploadFileName()[i]); int length=fileInputStream.read(bytes); while (length>0){ //Write the contents of length each time fileOutputStream.write(bytes,0,length); length=fileInputStream.read(bytes); } fileInputStream.close(); fileOutputStream.flush(); fileOutputStream.close(); } return SUCCESS; } }
Multi-file upload, you can also use multiple File controls, different name attributes, but in this way, each additional File control, you must increase the settings of attributes accordingly, not recommended to use.
File Download
To support file download, Struts2 framework provides stream result type, which is used to implement file download function.
stream result type
stream configuration parameters
Name | Effect |
---|---|
contentType | Set the MIME type sent to the browser |
contentLength | Set file size |
contentDisposition | Set the value of the Content-Disposition parameter in the HTTP header information of the response |
inputName | Specify the attribute name of the inputStream type provided in Action |
bufferSize | Set the buffer size for reading and downloading files |
<a href="download?fileName=jietu.png">download</a>
<!--Download files--> <action name="download" class="com.bdqn.Control.DownFileAction"> <param name="inputPath">/uploads</param> <result name="success" type="stream"> <param name="contentType">application/octet-stream</param> <param name="inputName">inputStream</param> <param name="contentDisposition">attachment;fileName="${fileName}"</param> <param name="bufferSize">4096</param> </result> </action>
package com.bdqn.Control; import com.opensymphony.xwork2.ActionSupport; import org.apache.struts2.ServletActionContext; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.InputStream; public class DownFileAction extends ActionSupport { //Read the directory of downloaded files private String inputPath; //File name of download file private String fileName; //Input stream for downloading files private InputStream inputStream; //Types in download files private String contenttype; public String getInputPath() { return inputPath; } public void setInputPath(String inputPath) { this.inputPath = inputPath; } public String getFileName() { return fileName; } public void setFileName(String fileName) { this.fileName = fileName; } public InputStream getInputStream() throws FileNotFoundException { String path= ServletActionContext.getServletContext().getRealPath(inputPath); return new BufferedInputStream(new FileInputStream(path+"\\"+fileName)); } public void setInputStream(InputStream inputStream) { this.inputStream = inputStream; } public String getContenttype() { return contenttype; } public void setContenttype(String contenttype) { this.contenttype = contenttype; } @Override public String execute() throws Exception { return SUCCESS; } }