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();
    }
    }
    }


  • 相关阅读:
    诸葛亮的后半生:狗笼子里挥舞丈八蛇矛
    一句话摘录
    【书摘】The Joshua tree epiphany
    玩具程序:bigInt
    旅行的力量
    记忆的力量
    快的力量
    Windbg学习笔记【4】
    戴尔笔记本win8全新安装
    悟透JavaScript
  • 原文地址:https://www.cnblogs.com/jimmy-muyuan/p/5715946.html
Copyright © 2011-2022 走看看