Java struts2 getting started-two ways to download files

Source: Internet
Author: User

I. about File Download: the core idea of File Download is to copy an object from one place to another. 1. traditional Method: Add a large number of servlet api operations to the Action. the advantage is good understanding, and the disadvantage is high coupling. 2. stream method: Use the stream interceptor in struts2 for operation 2. example: I use maven and paste the pom. xml: Copy Code <project xmlns = "http://maven.apache.org/POM/4.0.0" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi: schemaLocation = "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion> 4.0.0 </modelVersion> <groupId> com. amos </groupId> <artifactId> struts2_learn </artifactId> <packaging> war </packaging> <Version> 0.0.1-SNAPSHOT </version> <name> struts2_learn Maven Webapp </name> <url> http://maven.apache.org </url> <dependencies> <dependency> <groupId> junit </ groupId> <artifactId> junit </artifactId> <version> 4.11 </version> <scope> test </scope> </dependency> <groupId> org. apache. struts </groupId> <artifactId> struts2-core </artifactId> <version> 2.3.16 </version> </dependency> <groupId> Org. apache. commons </groupId> <artifactId> commons-io </artifactId> <version> 1.3.2 </version> </dependency> <groupId> jstl </groupId> <artifactId> jstl </artifactId> <version> 1.1.2 </version> <scope> provided </scope> </dependency> <groupId> taglibs </groupId> <artifactId> standard </artifactId> <version> 1.1.2 </version> </dependency> </dependencies> <build> <finalName> struts2_learn </fi NalName> </build> </project> copy Code 1. downloadAction. java copy code package download; import java. io. fileInputStream; import java. io. inputStream; import java. io. outputStream; import java. io. unsupportedEncodingException; import java.net. URLEncoder; import javax. servlet. servletContext; import javax. servlet. http. httpServletResponse; import org. apache. struts2.ServletActionContext; import com. opensymphony. xw Ork2.ActionSupport;/*** @ ClassName: DownloadAction * @ Description: File Download * @ author: amosli * @ email: amosli@infomorrow.com * @ date Feb 13,201 4 1:22:23 AM */public class DownloadAction extends ActionSupport {private static final long serialVersionUID =-5609061774544252181l; private String fileName; // file name public void setFileName (String fileName) {if (ServletActionContext. getRequest (). getMethod (). equ Als ("GET") {try {byte [] bytes = fileName. getBytes ("ISO8859-1"); // process the Chinese character submitted by the get method, converting the encoding from the ISO8859-1 to UTF-8 fileName = new String (bytes, "UTF-8 ");} catch (UnsupportedEncodingException e) {e. printStackTrace () ;}} this. fileName = fileName;} // download public String execute () throws Exception {// 1. traditional download method // obtain the HttpServletResponse object HttpServletResponse response = ServletActionContext. getResponse (); // get The ServletContext object ServletContext context = ServletActionContext. getServletContext (); // notify the browser to open the file response as a download. setHeader ("content-disposition", "attachment; filename =" + URLEncoder. encode (fileName, "UTF-8"); // obtain the root directory String realPath = context. getRealPath ("/WEB-INF/download"); // construct the subnode input stream InputStream is = new FileInputStream (realPath + "/" + fileName ); // construct the output stream OutputStream OS = response. g EtOutputStream (); byte [] B = new byte [1024]; int len = 0; while (len = is. read (B)> 0) {OS. write (B, 0, len);} is. close (); OS. close (); return SUCCESS ;}} copy the code download. jsp copy the Code <% @ page language = "java" contentType = "text/html; charset = UTF-8" pageEncoding = "UTF-8" %> <% @ taglib uri = 'HTTP: // java.sun.com/jsp/jstl/core' prefix = "c" %> <! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd">

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.