• SpringMvc之java文件下载


    首先强调,需要下载的文件只能放在项目中的webapp下

    1、页面的一个超链接,链接到controller

    <a href="<%=path%>/download">点击下载文件</a>

    2、controller中的代码:

    @RequestMapping("/download")
        @ResponseBody
            public void downLoadExcelModel(HttpServletRequest request,HttpServletResponse response) throws Exception {
             String download = request.getSession().getServletContext().getRealPath("/upload/"); //获取下载路劲
             ExcelAndCsvDownload.downLoadFile(moban.csv,csv,download, response);//依次传入需要下载的文件名,文件格式,路径,response参数
           }

    3、工具类:

    package com.common.util;
    
    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import javax.servlet.http.HttpServletResponse;
    public class ExcelAndCsvDownload{
            public static boolean downLoadFile(String name,String type,String path,HttpServletResponse response)
                throws Exception {
                String fileName = name;
                String fileType = type;
                File file = new File(path+fileName);  //根据文件路径获得File文件
                //设置文件类型(这样设置就不止是下Excel文件了,一举多得)
                if("pdf".equals(fileType)){
                   response.setContentType("application/pdf;charset=GBK");
                }else if("csv".equals(fileType)){
                   response.setContentType("application/msexcel;charset=GBK");
                }else if("doc".equals(fileType)){
                   response.setContentType("application/msword;charset=GBK");
                }else if("xls".equals(fileType)){
                       response.setContentType("application/msexcel;charset=GBK");
                    }
                //文件名
                response.setHeader("Content-Disposition", "attachment;filename=""
                    + new String(fileName.getBytes(), "ISO8859-1") + """);
                response.setContentLength((int) file.length());
                byte[] buffer = new byte[4096];// 缓冲区
                BufferedOutputStream output = null;
                BufferedInputStream input = null;
                try {
                  output = new BufferedOutputStream(response.getOutputStream());
                  input = new BufferedInputStream(new FileInputStream(file));
                  int n = -1;
                  //遍历,开始下载
                  while ((n = input.read(buffer, 0, 4096)) > -1) {
                     output.write(buffer, 0, n);
                  }
                  output.flush();   //不可少
                  response.flushBuffer();//不可少
                } catch (Exception e) {
                  //异常自己捕捉       
                } finally {
                   //关闭流,不可少
                   if (input != null)
                        input.close();
                   if (output != null)
                        output.close();
                }
               return false;
            }
    }
  • 相关阅读:
    求列表中指定元素的位置
    Hash_P1026毒药?解药?
    Hash_集合
    bzoj1483: [HNOI2009]梦幻布丁
    bzoj1724: [Usaco2006 Nov]Fence Repair 切割木板
    容斥原理
    bzoj1042: [HAOI2008]硬币购物
    [Noi2016十连测第五场]二进制的世界
    NOI2016模拟赛Zbox loves stack
    bzoj2038: [2009国家集训队]小Z的袜子(hose)
  • 原文地址:https://www.cnblogs.com/guokai870510826/p/5792777.html
Copyright © 2020-2023  润新知