java 下载示例

 1 package com.charmyin.cmstudio.common.utils;
 2 
 3 import java.io.BufferedInputStream;
 4 import java.io.BufferedOutputStream;
 5 import java.io.File;
 6 import java.io.FileInputStream;
 7 import java.io.IOException;
 8 import java.io.InputStream;
 9 import java.io.OutputStream;
10 
11 import javax.servlet.http.HttpServletResponse;
12 
13 public class DownLoadExcelUtil {
14 
15     
16     public static void downLoadFile(String filePath, HttpServletResponse response, String fileName, String fileType)
17             throws Exception {
18             File file = new File(filePath);  //根据文件路径获得File文件
19             //设置文件类型(这样设置就不止是下Excel文件了,一举多得)
20             if("pdf".equals(fileType)){
21                response.setContentType("application/pdf;charset=GBK");
22             }else if("xls".equals(fileType)){
23                response.setContentType("application/msexcel;charset=GBK");
24             }else if("doc".equals(fileType)){
25                response.setContentType("application/msword;charset=GBK");
26             }
27             //文件名
28             response.setHeader("Content-Disposition", "attachment;filename=""
29                 + new String(fileName.getBytes(), "ISO8859-1") + """);
30             response.setContentLength((int) file.length());
31             byte[] buffer = new byte[4096];// 缓冲区
32             BufferedOutputStream output = null;
33             BufferedInputStream input = null;
34             try {
35               output = new BufferedOutputStream(response.getOutputStream());
36               input = new BufferedInputStream(new FileInputStream(file));
37               int n = -1;
38               //遍历,开始下载
39               while ((n = input.read(buffer, 0, 4096)) > -1) {
40                  output.write(buffer, 0, n);
41               }
42               output.flush();   //不可少
43               response.flushBuffer();//不可少
44             } catch (Exception e) {
45               //异常自己捕捉       
46             } finally {
47                //关闭流,不可少
48                if (input != null)
49                     input.close();
50                if (output != null)
51                     output.close();
52             }
53         }
54     
55      public static HttpServletResponse download(String path, HttpServletResponse response) {
56             try {
57                 // path是指欲下载的文件的路径。
58                 File file = new File(path);
59                 // 取得文件名。
60                 String filename = file.getName();
61                 // 取得文件的后缀名。
62                 String ext = filename.substring(filename.lastIndexOf(".") + 1).toUpperCase();
63 
64                 // 以流的形式下载文件。
65                 InputStream fis = new BufferedInputStream(new FileInputStream(path));
66                 byte[] buffer = new byte[fis.available()];
67                 fis.read(buffer);
68                 fis.close();
69                 // 清空response
70                 response.reset();
71                 // 设置response的Header
72                 response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes()));
73                 response.addHeader("Content-Length", "" + file.length());
74                 OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
75                 response.setContentType("application/octet-stream");
76                 toClient.write(buffer);
77                 toClient.flush();
78                 toClient.close();
79             } catch (IOException ex) {
80                 ex.printStackTrace();
81             }
82             return response;
83         }
84 }
原文地址:https://www.cnblogs.com/MDK-L/p/4030086.html