SpringMvc之java文件下载

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

 

一、页面的一个超连接,连接到controllerweb

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

二、controller中的代码:app

@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参数
       }

三、工具类:webapp

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;
        }
}
相关文章
相关标签/搜索