• 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;
            }
    }
  • 相关阅读:
    OpenGL(十一) BMP真彩文件的显示和复制操作
    OpenGL(十) 截屏并保存BMP文件
    复数的认识与理解
    复数的认识与理解
    采样定理
    采样定理
    How Many Fibs_hdu_1316(大数).java
    FZOJ2110 star(DFS)
    透过表象看本质!?之二数据拟合
    设计中的道理_3
  • 原文地址:https://www.cnblogs.com/guokai870510826/p/5792777.html
Copyright © 2020-2023  润新知