- package com.figo.action;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.UnsupportedEncodingException;
- import org.apache.commons.io.FileUtils;
- import org.apache.struts2.ServletActionContext;
- import org.apache.struts2.convention.annotation.ParentPackage;
- import org.apache.struts2.convention.annotation.Results;
- import org.apache.struts2.convention.annotation.Result;
- import com.opensymphony.xwork2.ActionSupport;
- import com.opensymphony.xwork2.ModelDriven;
- import com.opensymphony.xwork2.Validateable;
- import com.opensymphony.xwork2.ValidationAwareSupport;
- /**
- * 所下载文件相关的的几个属性:文件格式 contentType,
- * 获取文件名的方法inputName,
- * 文件内容(显示的)属性contentDisposition,
- * 限定下载文件 缓冲区的值bufferSize
- * */
- @Results({ @Result(name = "success", type = "stream", params = { "contentType",
- "application/octet-stream;charset=ISO8859-1", "inputName",
- "inputStream", "contentDisposition",
- "attachment;filename=\"Readme.txt\"", "bufferSize", "4096" }) })
- public class DownloadAction extends ActionSupport {
- private static final long serialVersionUID = 8784555891643520648L;
- private String STORAGEPATH = "/upload/Readme.txt";
- private String fileName;// 初始的通过param指定的文件名属性
- private String storageId;
- private String inputPath;// 指定要被下载的文件路径
- public String getFileName() {
- return fileName;
- }
- public void setFileName(String fileName) {
- this.fileName = fileName;
- }
- public void setInputPath(String inputPath) {
- this.inputPath = inputPath;
- }
- public String getStorageId() {
- return storageId;
- }
- public void setStorageId(String storageId) {
- this.storageId = storageId;
- }
- // 如果下载文件名为中文,进行字符编码转换
- public String getDownloadFileName() {
- String downloadFileName = fileName;
- try {
- downloadFileName = new String(downloadFileName.getBytes(),
- "ISO8859-1");
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
- }
- return downloadFileName;
- }
- public InputStream getInputStream() throws Exception {
- /**
- * 下载用的Action应该返回一个InputStream实例,
- * 该方法对应在result里的inputName属性值为targetFile
- **/
- return ServletActionContext.getServletContext().getResourceAsStream(
- STORAGEPATH);
- }
- public String execute() throws Exception {
- return SUCCESS;
- }
- }
- <%@ page language="java" contentType="text/html; charset=utf-8"
- pageEncoding="utf-8"%>
- <%@ taglib prefix="s" uri="/struts-tags"%>
- <! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <title>Download</title>
- </head>
- <body>
- <s:a href="download.action">Download</s:a>
- </body>
- </html>
代码其实很简单,最关键的就是getInputStream()方法,返回一个InputStream实例,还有就是@Results的配置。
测试页面:download.jsp(注意:这段代码使用了struts2的约定方式,在JSP页面中的action名称为download.action,利用约定自动找到名为DownloadAction的类进行处理)。另外在web.xml中要将拦截器的匹配方式改为/*。
图中包的名称为,此时action的名称前不需要配命名空间,但如果写成第二行的包名称,那么action名称应该为test/download
另外,文中需要下载的资源放在项目文件下,即/upload/Readme.txt",所以获得下载路径可以用
- ServletActionContext.getServletContext().getResourceAsStream(
- STORAGEPATH);
但是,如果下载文件的路径path="E:\\Readme.txt"这样的,那么上段代码就改成
return new FileInputStream(path);