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 ...
随机推荐
- requests记录
http://docs.python-requests.org/zh_CN/latest/user/quickstart.html r = requests.get('http://httpbin.o ...
- git 代码回滚与爬坑 -- reset and revert
本文通过MetaWeblog自动发布,原文及更新链接:https://extendswind.top/posts/technical/git_code_roll_back_revert_and_res ...
- Python的十种常见算法
十种排序算法 1. 常见算法分类 十种常见排序算法一般分为以下几种: (1)非线性时间比较类排序: a. 交换类排序(快速排序.冒泡排序) b. 插入类排序(简单插入排序.希尔排序) c. ...
- MongoDB之安装部署
一.安装MongoDB 在安装MongoDB之前,应该先把MongoDB官方网站上下载下来,下载的地址如下: https://www.mongodb.com/download-center 下载完毕之 ...
- mysql5.7 源码安装步骤
操作系统:centos 7 mysql版本:5.7 下载地址:https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.24-linux-gli ...
- 2018-2019-2 20165222《网络对抗技术》Exp9 Web安全基础
1.实践过程记录 1.字符串型注入. 2.整数型注入 3.注入语句查看其他内容 4.xss是一种漏洞,这种漏洞允许用户输入脚本并且浏览器提交的时候不加编码.这种东西是最为流行并且有害的web应用的问题 ...
- django + vue3 解决跨越问题
django跨域 解决: https://yq.aliyun.com/articles/517215 vue3 跨越(此处没必要,django处理即可): https://blog.csdn.net/ ...
- Go --- 七牛云 上传文件 & Token demo
package main import ( "bytes" "crypto/hmac" "crypto/sha1" "encodi ...
- docker - nginx+php+php-mysql(扩展)
Docker 安装 Nginx(https://www.runoob.com/docker/docker-install-nginx.html) Docker 安装 PHP(https://www.r ...
- 【Canvas】勾画调和级数Harmonic series 曲线 y=1+1/2+1/3+1/4+1/5+1/6+1/7+1/8+....
相关资料:https://baike.baidu.com/item/%E8%B0%83%E5%92%8C%E7%BA%A7%E6%95%B0/8019971?fr=aladdin 调和级数(英语:Ha ...