Upload and download Struts2 files, and upload struts2 files

Source: Internet
Author: User

Upload and download Struts2 files, and upload struts2 files

I. Preface

As the Struts2 framework encapsulates the Upload components, the file upload implementation is simple.

Support for the commons-fileupload component is provided in the Struts2 framework, and this component is used by default to upload files. Therefore, to implement the file upload function, we need to include the following two jar files in the project:

Commons-fileupload-x.x.x.jar, commons-io-x.x.x.jar note: the jar file version depends on the version of Struts2 used by the current project.

Ii. Implement File Upload

1) Prepare the upload page

<% @ Page language = "java" import = "java. util. * "pageEncoding =" UTF-8 "%> <% String path = request. getContextPath (); String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/"; %> <% @ taglib uri = "/struts-tags" prefix = "s" %> <! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN"> 

2) Development and implementation of File Upload actions

Package cn. happy. action; import java. io. fileInputStream; import java. io. fileOutputStream; import org. apache. struts2.ServletActionContext; import com. opensymphony. xwork2.Action; import com. opensymphony. xwork2.ActionSupport; @ SuppressWarnings ("serial") public class UploadAction extends ActionSupport implements Action {// encapsulate the upload File Attribute private String upload; // encapsulate the upload file type private String uploadContentType; // encapsulate the name of the uploaded file private String uploadFileName; // obtain the File Upload path private String savePath; @ Override public String execute () throws Exception {byte [] buffer = new byte [1024]; // read the file FileInputStream FD = new FileInputStream (getUpload (); // save the file, and set the path FileOutputStream fos = new FileOutputStream (getSavePath () + "\" + this. getUploadFileName (); int length = FCM. read (buffer); while (length> 0) {// each write length of the content fos. write (buffer, 0, length); length = Sox. read (buffer. close (); fos. flush (); fos. close (); return SUCCESS;} // obtain the Save path of the uploaded file // obtain the Save path from the read storage directory public String getSavePath () {return ServletActionContext. getServletContext (). getRealPath (savePath);} public void setSavePath (String savePath) {this. savePath = savePath;} public String getUpload () {return upload;} public void setUpload (String 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 ;}}

3) Struts. xml configuration

<? Xml version = "1.0" encoding = "UTF-8"?> <! DOCTYPE struts PUBLIC "-// Apache Software Foundation // DTD Struts Configuration 2.3 // EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <! -- <Constant name = "struts. devMode" value = "true"/> --> <constant name = "struts. devMode" value = "false"/> <! -- Create a default package, struts-default package inherited from Struts2 --> <package name = "default" namespace = "" extends = "struts-default"> <action name = "upload" class = "cn. happy. action. uploadAction "> <! -- Use the param parameter to save the directory path --> <param name = "savePath">/upload </param> <result name = "success"> upload_success.jsp </result> </action> </package> </struts>

4) on the success page

<% @ Page language = "java" import = "java. util. * "pageEncoding =" UTF-8 "%> <% String path = request. getContextPath (); String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/"; %> <% @ taglib uri = "/struts-tags" prefix = "s" %> <! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN"> 

3. Implement File Download

1) download page preparation

<% @ Page language = "java" import = "java. util. * "pageEncoding =" UTF-8 "%> <% String path = request. getContextPath (); String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/"; %> <% @ taglib uri = "/struts-tags" prefix = "s" %> <! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN"> 

2) start to implement the Action for File Download

Package cn. happy. action; import java. io. bufferedInputStream; import java. io. fileInputStream; import java. io. fileNotFoundException; import java. io. inputStream; import org. apache. struts2.ServletActionContext; import com. opensymphony. xwork2.ActionSupport; @ SuppressWarnings ("serial") public class FileDownAction extends ActionSupport {// read the downloaded file directory private String inputPath; // download the file name private String fileName; // read the input stream of the downloaded file @ SuppressWarnings ("unused") private InputStream inputStream; // The downloaded file type private String conetntType; // create inputStream input stream @ Override public String execute () throws Exception {return SUCCESS;} 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 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 String getConetntType () {return conetntType;} public void setConetntType (String conetntType) {this. conetntType = conetntType ;}}

3) Struts. xml configuration

<? Xml version = "1.0" encoding = "UTF-8"?> <! DOCTYPE struts PUBLIC "-// Apache Software Foundation // DTD Struts Configuration 2.3 // EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <! -- <Constant name = "struts. devMode" value = "true"/> --> <constant name = "struts. devMode" value = "false"/> <! -- Create a default package, struts-default package inherited from Struts2 --> <package name = "default" namespace = "" extends = "struts-default"> <action name = "upload" class = "cn. happy. action. uploadAction "> <! -- File download --> <action name = "download" class = "cn. happy. action. fileDownAction "> <param name =" inputPath ">/upload </param> <result name =" success "type =" stream "> <param name =" conetntType "> application/ octet-stream </param> <! -- <Param name = "conetntType"> image/gif </param> --> <param name = "inputName"> inputStream </param> <param name = "contentDisposition"> attachement; filename = "$ {fileName}" </param> <param name = "bufferSize"> 4096 </param> </result> </action> </package> </struts>

Note !!!!

File type corresponding to contentType

File Type ContentType settings
Word Application/msword
Execl Application/vnd. ms-excel
PPT Application/vnd. ms-powerpoint
Image Image/gif, image/bmp, image/jpeg
Text Files Text/plain
HTML webpage Text/html
Executable files Application/octet-stream

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.