zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    抽奖代码
    org.hibernate.AssertionFailure: null id in com.you.model.User entry (don't flush the Session after a
    Cannot add or update a child row: a foreign key constraint fails
    SyntaxError:identifier starts immediately after numeric literal
    too much recursion
    微信处理红包
    minerd
    minerd
    kill常用
    阿里云centos 6安装Nginx+PHP+MySQL
  • 原文地址:https://www.cnblogs.com/god-monk/p/7994578.html
Copyright © 2011-2022 走看看