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


  • 相关阅读:
    iOS 9 新特性 UIStackView
    自定义 URL Scheme 完全指南
    使用NSURLCache缓存
    swift 3.0 新特征
    《转之微信移动团队微信公众号》iOS 事件处理机制与图像渲染过程
    《转》使用NSURLSession发送GET和POST请求
    《转》IOS 扩展 (Extension)
    《转》__block修饰符
    《转》Objective-C Runtime(4)- 成员变量与属性
    《转》Objective-C Runtime(3)- 消息 和 Category
  • 原文地址:https://www.cnblogs.com/jimmy-muyuan/p/5715946.html
Copyright © 2011-2022 走看看