Java 文件下载工具类
Java 文件下载工具类
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
private static Logger logger = LoggerFactory.getLogger(DownloadUtil.class);
文件下载方法
/**
* 文件下载方法
* @param response
* @param filePath
* @param encode
*/
public static void download(HttpServletResponse response, String filePath, String encode) {
response.setContentType("text/html;charset=" + encode);
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
String downLoadPath = filePath;
try {
File file = new File(downLoadPath);
long fileLength = file.length();
String fileName = file.getName();
response.setContentType("application/x-msdownload;");
response.setHeader("Content-disposition", "attachment; filename=" + new String(fileName.getBytes(encode), "ISO8859-1"));
response.setHeader("Content-Length", String.valueOf(fileLength));
bis = new BufferedInputStream(new FileInputStream(downLoadPath));
bos = new BufferedOutputStream(response.getOutputStream());
byte[] buff = new byte[2048];
int bytesRead;
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
} catch (Exception e) {
logger.error(e.getMessage());
} finally {
if (bis != null)
try {
bis.close();
} catch (IOException e) {
logger.error(e.getMessage());
}
if (bos != null)
try {
bos.close();
} catch (IOException e) {
logger.error(e.getMessage());
}
}
}
以流的方式下载
/**
* 以流的方式下载
* @param response
* @param filePath
* @param encode
* @return response
*/
public static HttpServletResponse downloadStream(HttpServletResponse response, String filePath, String encode) {
response.setContentType("text/html;charset=" + encode);
try {
// path是指欲下载的文件的路径
File file = new File(filePath);
// 取得文件名
String filename = file.getName();
// 取得文件的后缀名
// String ext = filename.substring(filename.lastIndexOf(".") + 1).toUpperCase();
// 以流的形式下载文件
InputStream fis = new BufferedInputStream(new FileInputStream(filePath));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
// 清空response
response.reset();
// 设置response的Header
response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes(encode), "ISO8859-1"));
response.addHeader("Content-Length", "" + file.length());
OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
toClient.write(buffer);
toClient.flush();
toClient.close();
} catch (IOException ex) {
logger.error(ex.getMessage());
}
return response;
}
下载本地文件
/**
* 下载本地文件
* @param response
* @param filePath
* @param encode
*/
public static void downloadLocal(HttpServletResponse response, String filePath,String encode) {
response.setContentType("text/html;charset=" + encode);
try {
// 读到流中
InputStream inStream = new FileInputStream(filePath); // 文件的存放路径
// path是指欲下载的文件的路径
File file = new File(filePath);
// 取得文件名
String fileName = file.getName();
// 设置输出的格式
response.reset();
response.setContentType("bin");
response.addHeader("Content-Disposition", "attachment; filename=\"" + new String(fileName.getBytes(encode), "ISO8859-1") + "\"");
// 循环取出流中的数据
byte[] b = new byte[100];
int len;
while ((len = inStream.read(b)) > 0) {
response.getOutputStream().write(b, 0, len);
}
inStream.close();
} catch (IOException e) {
logger.error(e.getMessage());
}
}
Java 文件下载工具类的更多相关文章
- Java Properties工具类详解
1.Java Properties工具类位于java.util.Properties,该工具类的使用极其简单方便.首先该类是继承自 Hashtable<Object,Object> 这就奠 ...
- Java json工具类,jackson工具类,ObjectMapper工具类
Java json工具类,jackson工具类,ObjectMapper工具类 >>>>>>>>>>>>>>> ...
- Java日期工具类,Java时间工具类,Java时间格式化
Java日期工具类,Java时间工具类,Java时间格式化 >>>>>>>>>>>>>>>>>&g ...
- Java并发工具类 - CountDownLatch
Java并发工具类 - CountDownLatch 1.简介 CountDownLatch是Java1.5之后引入的Java并发工具类,放在java.util.concurrent包下面 http: ...
- MinerUtil.java 爬虫工具类
MinerUtil.java 爬虫工具类 package com.iteye.injavawetrust.miner; import java.io.File; import java.io.File ...
- MinerDB.java 数据库工具类
MinerDB.java 数据库工具类 package com.iteye.injavawetrust.miner; import java.sql.Connection; import java.s ...
- 小记Java时间工具类
小记Java时间工具类 废话不多说,这里主要记录以下几个工具 两个时间只差(Data) 获取时间的格式 格式化时间 返回String 两个时间只差(String) 获取两个时间之间的日期.月份.年份 ...
- Java Cookie工具类,Java CookieUtils 工具类,Java如何增加Cookie
Java Cookie工具类,Java CookieUtils 工具类,Java如何增加Cookie >>>>>>>>>>>>& ...
- UrlUtils工具类,Java URL工具类,Java URL链接工具类
UrlUtils工具类,Java URL工具类,Java URL链接工具类 >>>>>>>>>>>>>>>&g ...
随机推荐
- 浅谈CLOSE_WAIT
浅谈CLOSE_WAIT 发表于2016-01-19 TCP 有很多连接状态,每一个都够聊十块钱儿的,比如我们以前讨论过 TIME_WAIT 和 FIN_WAIT1,最近时不时听人提起 CLOSE_W ...
- C++类*类型和其他类型相互转换
类类型转换时会出现两种之间转换,下面我们说的是类类型 1.其他类型转换为本类类型 通过类带一个参数的构造函数:或者多个参数构造函数,除了第一个参数后面参数都有默认值时!这样在其他类型赋值给该类类型对象 ...
- [ZJOI2004]嗅探器 (割点)
这题就比较好玩吧水题 以数据范围来看随便怎么做就能过 \(O(n)\)显然我们得过一个割点,其次这个割点得在\(x-y\)中间且不为始终点 其他都好说,在中间:从\(x\)开始遍历,首先得保证\(x- ...
- 鲁班学院java架构vip课程
1.wps文档地址 https://docs.qq.com/doc/DRVNLUndvTmFSdEhO 2.百度网盘地址 https://pan.baidu.com/s/1uxaTzJZHKrsw_H ...
- zabbix (11) 监控TCP连接数
对TCP的监控可以采用ss.netstat./proc/net/tcp这三个不同的方案来实现.其中ss是最快的 (1)ss命令 [root@manager1 script_py ::]#time ss ...
- 国内Archlinux arm的镜像源
清华 http://mirrors.tuna.tsinghua.edu.cn/archlinuxarm/arch/arch/repo 中科大 http://mirrors.ustc.edu.cn/ar ...
- Result window is too large, from + size must be less than or equal to [10000]
使用sql插件执行如下语句的时候报错http://10.127.0.1:9200/_sql?sql=select * from test limit 1000000 错误信息:{"error ...
- java.sql.SQLException: The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the server
错误信息 java.sql.SQLException: The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents ...
- LayUI使用弹窗重载父级数据表格的两种方法
参考LayUI官方文档,在子窗口中重载父级数据表格找到以下两种方法: 1.子窗口中重载:在子窗口中直接调用父级talbe的reload方法. $("body").on(" ...
- 寻找丢失的微服务-HAProxy热加载问题的发现与分析 原创: 单既喜 一点大数据技术团队 4月8日 在一点资讯的容器计算平台中,我们通过HAProxy进行Marathon服务发现。本文记录HAProxy服务热加载后某微服务50%概率失效的问题。设计3组对比实验,验证了陈旧配置的HAProxy在Reload时没有退出进而导致微服务丢失,并给出了解决方案. Keywords:HAProxy热加
寻找丢失的微服务-HAProxy热加载问题的发现与分析 原创: 单既喜 一点大数据技术团队 4月8日 在一点资讯的容器计算平台中,我们通过HAProxy进行Marathon服务发现.本文记录HAPro ...