zoukankan      html  css  js  c++  java
  • 服务器下载文件

    一、通过struts配置文件下载

    1.前台页面:

    <li>  <a href="cbim/CbimAction!downloadFiles.action" target="_blank" >模板下载</a>   </li>

    2.后台action:

     private InputStream inputStream;//get,set方法
    
      private String fileName;//get,set方法
    
      public String downloadFiles(){
    
      String path="D://导入模板/信息导入模板-20160215.xlsx";
    
      try {
    
        inputStream =new FileInputStream(path);
    
        fileName=new String("信息导入模板.xlsx".getBytes(),"ISO8859-1");
    
      } catch (FileNotFoundException e) {
    
        e.printStackTrace();
    
      }catch (UnsupportedEncodingException e) {
    
        e.printStackTrace();
    
      }
    
    return "downloadFiles";
    
    }

    3.struts配置文件:

     <result name="downloadFiles" type="stream">
    
        <!--指定下载文件的类型  -->
    
        <param name="contentType">application/vnd.ms-excel</param>
    
        <!-- inputName默认值是inputStream -->
    
        <param name="inputName">inputStream</param>
    
        <param name="contentDisposition">
    
           attachment;filename="${fileName}"
    
        </param>
    
        <param name="bufferSize">2048</param>
    
      </result>

    二、通过HttpServletResponse下载:

     public void downloadDataPdf() throws Exception {
        InputStream is = new BufferedInputStream(new FileInputStream(new File("D://毕业证.pdf"))); HttpServletResponse response = ServletActionContext.getResponse(); response.setContentType("application/pdf"); OutputStream os = response.getOutputStream(); byte[] readByte = new byte[4096]; int c; while ((c = is.read(readByte)) != -1) { os.write(readByte,0,c); } os.flush(); if (os != null) { os.close(); } if (is != null) { is.close(); }
    }
    public void downloadDataPdf() throws Exception {
        InputStream is = new BufferedInputStream(new FileInputStream(new File("D://毕业证.pdf"))); HttpServletResponse response = ServletActionContext.getResponse(); response.setContentType("application/pdf"); OutputStream os = renderExcel(response, "毕结业证书.pdf"); byte[] readByte = new byte[4096]; int c; while ((c = is.read(readByte)) != -1) { os.write(readByte,0,c); } os.flush(); if (os != null) { os.close(); } if (is != null) { is.close(); }
    }

    public OutputStream renderExcel(HttpServletResponse response, String docName) {
      return render("application/x-msdownload;charset=UTF-8", docName, response);
    }

    public static OutputStream render(String contentType, String docName, HttpServletResponse response) {
      OutputStream outStream = null;
      try {
        response.setContentType(contentType);
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        String agent = ServletActionContext.getRequest().getHeader("USER-AGENT");
        // 处理火狐浏览器,文件名编码问题
        if (agent.indexOf("Firefox") > -1) {
          docName = new String(docName.getBytes("GB2312"), "ISO-8859-1");
        } else {
          docName = URLEncoder.encode(docName, "utf-8");
        }
        response.setHeader("Content-disposition", "attachment;filename=" + docName);
        outStream = response.getOutputStream();
      }catch (IOException e) {
        log.error(e.getMessage(), e);
      }
      return outStream;
    }

  • 相关阅读:
    【移动安全基础篇】——14、创建破解代码库
    【移动安全基础篇】——13、Android关键代码快速定位
    【移动安全基础篇】——12、Dalvik虚拟机
    【移动安全基础篇】——11、Android_jni
    【移动安全基础篇】——10、Android源代码修改
    【移动安全基础篇】——09、Android源代码目录结构
    【移动安全高级篇】————7、APK 的自我保护
    【移动安全高级篇】————6、Android DEX安全攻防战
    【移动安全高级篇】————5、Andorid APK反逆向解决方案---梆梆加固原理探寻
    Scene
  • 原文地址:https://www.cnblogs.com/zijinyouyou/p/5190728.html
Copyright © 2011-2022 走看看