Struts2实现特定文件下载

2014-11-24 09:53:48 · 作者: · 浏览: 0

对WebContent中download目录下的city.xml文件进行下载


package com.struts2.action;  
  
import java.io.InputStream;  
  
import org.apache.struts2.ServletActionContext;  
  
import com.opensymphony.xwork2.ActionSupport;  
  
public class DownloadAction extends ActionSupport {  
  
    private static final long serialVersionUID = 1L;  
  
    /** 文件名 */  
    private String fileName;  
  
    public String getFileName() {  
        return fileName;  
    }  
  
    public void setFileName(String fileName) {  
        this.fileName = fileName;  
    }  
  
    public InputStream getInputStream() throws Exception {  
  
        this.fileName = "city.xml";  
  
        return ServletActionContext.getServletContext().getResourceAsStream(  
                "/download/" + fileName);  
    }  
  
    @Override  
    public String execute() throws Exception {  
        return SUCCESS;  
    }  
  
}  


配置struts.xml文件

< xml version="1.0" encoding="UTF-8" >
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">







inputStream
attachment;filename="${fileName}"




< xml version="1.0" encoding="UTF-8" >
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">







inputStream
attachment;filename="${fileName}"