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


  • 相关阅读:
    UMLChina-我不经意的创业故事
    oracle management server
    关于做PDF的FAQ(一)~(四)
    关于学习ASP和编程的28个观点
    JavaScript的方法和技巧
    在公告栏里加进啦Google自定义搜索引擎(附代码,和参考代码,原代码)
    来北京工作了,有写感慨
    asp.net 2.0 访问oracle
    利用SharpZipLib进行文件的压缩和解压缩
    软件工程师,请不要做浮躁的人
  • 原文地址:https://www.cnblogs.com/jimmy-muyuan/p/5715946.html
Copyright © 2020-2023  润新知