Struts2文件下载

Posted 袋子里的袋鼠

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Struts2文件下载相关的知识,希望对你有一定的参考价值。

1.传统下载
public class DownloadAction extends ActionSupport {
    private String fileName;//文件名称
    public void setFileName(String fileName) {
        if (ServletActionContext.getRequest().getMethod().equals("GET")) {
            try {
                byte[] bytes = fileName.getBytes("ISO8859-1");//将get方式提交的中文进行处理,即将编码由ISO8859-1转为utf-8
                fileName = new String(bytes, "utf-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        this.fileName = fileName;
    }

    // 下载
    public String execute() throws Exception {
        // 1.传统下载方式
        // 取得HttpServletResponse对象
        HttpServletResponse response = ServletActionContext.getResponse();
        // 取得ServletContext对象
        ServletContext context = ServletActionContext.getServletContext();
        // 通知浏览器以下载方式打开文件
        response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "utf-8"));
        // 取得需要下载文件的根目录
        String realPath = context.getRealPath("/WEB-INF/download");
        // 构造子节输入流
        InputStream is = new FileInputStream(realPath + "/" + fileName);
        // 构造子节输出流
        OutputStream os = response.getOutputStream();
        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;
    }
}

2.文件下载

public class DownloadStreamAction extends ActionSupport {
    private static final long serialVersionUID = -2747191035343710583L;
    private String fileName;
    public void setFileName(String fileName) throws Exception {
        if(ServletActionContext.getRequest().getMethod().equals("GET")){
            byte[] bytes = fileName.getBytes("ISO8859-1");
            fileName=new String(bytes,"utf-8");
        }
        this.fileName = fileName;
    }
    public String getFileName() throws Exception {
        return URLEncoder.encode(fileName, "utf-8");
    }
    public InputStream getImageStream() throws Exception {
        InputStream inputStream = ServletActionContext.getServletContext().getResourceAsStream("/WEB-INF/download/"+fileName);
        return inputStream;
    }
}
 <package name="download" extends="struts-default">
      <action name="DownloadAction" class="download.DownloadStreamAction" method="execute">
         <!-- 以stream二进制流的方式打开 -->
           <result name="success" type="stream">
                <!-- 指明文件的下载类型 -->
                <param name="contentType">image/jpeg</param>    
                <!-- 指明如果取得需要下载文件的InputStream输入流 -->
                <param name="inputName">imageStream</param>    
                <!-- 指明让浏览器以下载框的方式打开 -->
                <param name="contentDisposition">attachment;filename="building.jpg"</param>    
                <!--<param name="contentDisposition">attachment;filename=${fileName}</param>-->    
                <!-- 指明下载文件时的字符数组byte[]大小 -->
                <param name="bufferSize">1024</param>
           </result>
      </action>
 </package>
转载自:https://www.cnblogs.com/amosli/p/3547414.html

以上是关于Struts2文件下载的主要内容,如果未能解决你的问题,请参考以下文章

免费下载全套最新011Struts2视频教程+教学资料+学习课件+源代码+软件开发工具

struts2实现文件上传和下载

struts2的文件配置以及注释代码

struts2的配置

Struts2基本使用--配置文件简述

struts2实现文件上传下载