zoukankan      html  css  js  c++  java
  • World、Excel利用流下载

    /**
    * 下载excel
    * @param request
    * @param response
    * @param filePath
    * @param fileName
    */
    public static void download(HttpServletRequest request, HttpServletResponse response, String filePath, String fileName) {
    response.setContentType("application/vnd.ms-excel");
    BufferedInputStream bis = null;
    BufferedOutputStream bos = null;
    try {
    String recommendedName = new String(fileName.getBytes(), "iso_8859_1");//设置文件名称的编码格式
    response.setHeader("Content-Disposition", "attachment; filename=" + recommendedName + ".xls");
    bis = new BufferedInputStream(new FileInputStream(filePath));
    bos = new BufferedOutputStream(response.getOutputStream());
    byte[] buff = new byte[2048];
    int bytesRead;
    while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
    bos.write(buff, 0, bytesRead);
    }
    } catch (Exception e) {
    System.out.println(e.getMessage());
    } finally {
    try {
    if (bis != null)
    bis.close();
    if (bos != null)
    bos.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }


    /**
    * 下载word
    * @param request
    * @param response
    * @param filePath
    * @param fileName
    */
    public void download(HttpServletRequest request, HttpServletResponse response, String filePath, String fileName) {
    response.setContentType("application/x-msdownload;charset=UTF-8");

    BufferedInputStream bis = null;
    BufferedOutputStream bos = null;

    try {
    response.setHeader("Content-disposition", "attachment; filename=" + new String(fileName.getBytes("gbk"), "ISO8859-1"));
    bis = new BufferedInputStream(new FileInputStream(filePath));
    bos = new BufferedOutputStream(response.getOutputStream());
    byte[] buff = new byte[2048];
    int bytesRead;
    while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
    bos.write(buff, 0, bytesRead);
    }
    } catch (Exception e) {
    System.out.println(e.getMessage());
    } finally {
    try {
    if (bis != null)
    bis.close();
    if (bos != null)
    bos.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }


  • 相关阅读:
    ecshop里的$_CFG从哪来的
    高效PHP程序必知的53个技巧
    Jquery结合datagrid框架
    PHP数据类型转换(字符转数字,数字转字符)
    php 操作数组 (合并,拆分,追加,查找,删除等)
    css3制作导航栏
    php日期转时间戳,指定日期转换成时间戳
    PHP 时间与字符串的相互转化
    php 生成.txt文件
    linux PHP yum 安装phpzie
  • 原文地址:https://www.cnblogs.com/jimmy-muyuan/p/5715946.html
Copyright © 2011-2022 走看看