zoukankan      html  css  js  c++  java
  • struts2 annotation方式的下载(实用版)

    import java.io.InputStream;
    import java.io.UnsupportedEncodingException;
    import org.apache.struts2.ServletActionContext;
    import org.apache.struts2.convention.annotation.Result;
    import org.apache.struts2.convention.annotation.ResultPath;
    import org.apache.struts2.convention.annotation.Results;
    import com.opensymphony.xwork2.ActionSupport;
    @ResultPath("/")
    @Results(value={
            @Result(name="success", type="stream",params={"contentType","application/octet-stream;charset=utf-8","inputName","inputStream","contentDisposition","attachment;filename=${fileName}","bufferSize","4096"})
    })
    public class DownloadAction extends ActionSupport {
        private String fileName;
        private InputStream inputStream;
        public void setFileName(String fileName) {
            this.fileName = fileName;
        }
        public InputStream getInputStream() {
            InputStream tempInputStream= (InputStream) ServletActionContext.getRequest().getAttribute("inputStream");
            String tempFileName= (String) ServletActionContext.getRequest().getAttribute("fileName");
            this.setFileName(tempFileName);
            return tempInputStream;
        }
    
        public String getFileName() {
            String temp=this.fileName;
            {        
                try {
                    this.fileName =  new String(temp.getBytes(), "ISO8859-1"); 
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }   
            }
            return fileName;
        }
    }
    public class DemoAction extends ActionSupport {
    	//通过在params中配置namespace参数,就可以改变action的命名空间
    	@Action(value="downloadDemo",results={@Result(name="downloadDemo", type="chain",params={"namespace","/"},location="download")})
    	public String downloadDemo() throws FileNotFoundException
    	{
    		InputStream inputStream=new FileInputStream(new File("c:/test.xls")) ;
    		ServletActionContext.getRequest().setAttribute("inputStream", inputStream);
    		ServletActionContext.getRequest().setAttribute("fileName", "中文test.xls");
    		return "downloadDemo";
    	}
    }
    

      还可以这样,将actionName也放入params里,而不是使用location

    public class DemoAction extends ActionSupport {
        //通过在params中配置namespace参数,就可以改变action的命名空间
        @Action(value="downloadDemo",results={@Result(name="downloadDemo", type="chain",params={"namespace","/","actionName","download"}})
        public String downloadDemo() throws FileNotFoundException
        {
            InputStream inputStream=new FileInputStream(new File("c:/test.xls")) ;
            ServletActionContext.getRequest().setAttribute("inputStream", inputStream);
            ServletActionContext.getRequest().setAttribute("fileName", "中文test.xls");
            return "downloadDemo";
        }
    }

     http://localhost:8080/wang/downloadDemo.action

  • 相关阅读:
    OPENCV图像变换-1
    OPENCV形态学操作1
    OPENCV基本滤波算法
    OSX下编译安装opencv3.1.0与opencv_contrib_master
    iOS8学习笔记-构建多视图应用程序
    iOS8学习笔记2--autolayout
    iOS学习笔记1--在xcode6以上的版本中不使用storyboard以及部分控件使用
    Objective-c学习笔记3
    objective-c学习笔记2
    objective-c学习笔记
  • 原文地址:https://www.cnblogs.com/passer1991/p/2970645.html
Copyright © 2011-2022 走看看