博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
文件下载工具类
阅读量:4546 次
发布时间:2019-06-08

本文共 4884 字,大约阅读时间需要 16 分钟。

1 @Component  2 public class DownLoadUtil {  3     /**  4      * 文件下载编码  5      * 该编码告诉浏览器文件名的编码方式,以防下载中文文件名时有乱码  6      */  7     private static String encoding = "utf-8";  8   9     /** 10      * 文件下载 11      * @param response 12      * @param filePath 文件在服务器上的路径,包含文件名 13      */ 14     public static void download(HttpServletResponse response, String filePath){ 15         File file = new File(filePath.toString()); 16         download(response, file, null, encoding); 17     } 18  19     /** 20      * 文件下载 21      * @param response 22      * @param filePath 文件在服务器上的路径,包括文件名称 23      * @param fileName 文件下载到浏览器的名称,如果不想让浏览器下载的文件名称和服务器上的文件名称一样,请设置该参数 24      */ 25     public static void download(HttpServletResponse response, String filePath, String fileName){ 26         File file = new File(filePath.toString()); 27         download(response, file, fileName, encoding); 28     } 29  30     /** 31      * 文件下载 32      * @param response 33      * @param filePath 文件在服务器上的路径,包括文件名称 34      * @param fileName 文件下载到浏览器的名称,如果不想让浏览器下载的文件名称和服务器上的文件名称一样,请设置该参数 35      * @param encoding 文件名称编码 36      */ 37     public static void download(HttpServletResponse response, String filePath, String fileName, String encoding){ 38         File file = new File(filePath.toString()); 39         download(response, file, fileName, encoding); 40     } 41  42     /** 43      * 文件下载 44      * @param response 45      * @param file 文件 46      */ 47     public static void download(HttpServletResponse response, File file) { 48         download(response, file, null, encoding); 49     } 50  51     /** 52      * 文件下载 53      * @param response 54      * @param file 文件 55      * @param fileName 文件下载到浏览器的名称,如果不想让浏览器下载的文件名称和服务器上的文件名称一样,请设置该参数 56      */ 57     public static void download(HttpServletResponse response, File file, String fileName) { 58         download(response, file, fileName, encoding); 59     } 60  61     /** 62      * 文件下载 63      * @param response 64      * @param file 文件 65      * @param fileName 文件下载到浏览器的名称,如果不想让浏览器下载的文件名称和服务器上的文件名称一样,请设置该参数 66      * @param encoding 文件名称编码 67      */ 68     public static void download(HttpServletResponse response, File file, String fileName, String encoding) { 69         if(file == null || !file.exists() || file.isDirectory()){ 70             return; 71         } 72  73         // 如果不指定文件下载到浏览器的名称,则使用文件的默认名称 74         if (StringUtils.isBlank(fileName)) { 75             fileName = file.getName(); 76         } 77  78         try { 79             InputStream is = new FileInputStream(file); 80             download(response, is, fileName, encoding); 81         } catch (IOException e) { 82             e.printStackTrace(); 83         } 84     } 85  86     /** 87      * 文件下载 88      * @param response 89      * @param is 文件输入流 90      * @param fileName 下载的文件名称 91      * @throws IOException 92      */ 93     public static void download(HttpServletResponse response, InputStream is, String fileName){ 94         download(response, is, fileName, encoding); 95     } 96  97     /** 98      * 文件下载 99      * @param response100      * @param is 文件输入流101      * @param fileName 下载的文件名称102      * @param encoding 编码格式103      */104     public static void download(HttpServletResponse response, InputStream is, String fileName, String encoding){105         if(is == null || StringUtils.isBlank(fileName)){106             return;107         }108 109         BufferedInputStream bis = null;110         OutputStream os = null;111         BufferedOutputStream bos = null;112 113         try{114             bis = new BufferedInputStream(is);115             os = response.getOutputStream();116             bos = new BufferedOutputStream(os);117             response.setContentType("application/octet-stream;charset=" + encoding);118             response.setCharacterEncoding(encoding);119             response.setHeader("Content-disposition", "attachment;filename="+ URLEncoder.encode(fileName, encoding));120             byte[] buffer = new byte[1024];121             int len = bis.read(buffer);122             while(len != -1){123                 bos.write(buffer, 0, len);124                 len = bis.read(buffer);125             }126 127             bos.flush();128         }catch(IOException e){129             e.printStackTrace();130         }finally{131             if(bis != null){132                 try{133                     bis.close();134                 }catch(IOException e){}135             }136 137             if(is != null){138                 try{139                     is.close();140                 }catch(IOException e){}141             }142         }143     }144 145     public static String getEncoding() {146         return encoding;147     }148 149     public static void setEncoding(String encoding) {150         DownLoadUtil.encoding = encoding;151     }152 }

 

转载于:https://www.cnblogs.com/rolayblog/p/11263065.html

你可能感兴趣的文章
Python正则表达式指南
查看>>
前端学习之JavaScript中的 NaN 与 isNaN
查看>>
chrome安装json view插件
查看>>
CSS div 高度满屏
查看>>
页面回发速度由 6 秒减少为 0.6 秒的真实案例!
查看>>
一种实现C++反射功能的想法(一)
查看>>
lvs+keepalived+nginx高性能负载均衡集群
查看>>
XXL-Job高可用集群搭建
查看>>
JDBC
查看>>
CodeForces - 123E Maze
查看>>
ZOJ 1709 Oil Deposits(dfs,连通块个数)
查看>>
安卓开源项目周报0308
查看>>
记可敬可佩的老车同志
查看>>
Maven in 5 Minutes(Windows)
查看>>
常用前端开发工具合集
查看>>
T-SQL:SQL Server-数据开发(经典)
查看>>
IOS 截取字符串
查看>>
键盘控制div移动并且解决停顿问题(原生js)
查看>>
矩阵快速幂优化线性递推
查看>>
基础网络流学习笔记
查看>>