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 ...
随机推荐
- Unity与Android之间的交互之AndroidManifest
https://blog.csdn.net/qq_15003505/article/details/70231975 AndroidManifest,中文名一般称之为清单文件.它描述了应用程序的组件的 ...
- vue中router-link的详细用法
官网文档地址:https://router.vuejs.org/zh/api/#to 今天项目突然有需求,让vue中的一个页面跳转到另一个页面 // 字符串 <router-link to=&q ...
- fatal: 'origin' does not appear to be a git repository
git push时报以下错误: fatal: 'origin' does not appear to be a git repository fatal: Could not read from re ...
- python pillow 处理图片
demo1 #打开图片,并随机添加一些椒盐噪声 from PIL import Image import numpy as np import matplotlib.pyplot as plt img ...
- C++标准库分析总结(七)——<Hashtable、Hash_set、Hash_multiset、unordered容器设计原则>
编译器对关联容器的实现有两个版本,上一节总结了以红黑树做为基础的实现版本,这一节总结以哈希表(hash table,散列表)为底部结构的实现版本. 一.Hashtable简单介绍 Hashtable相 ...
- 去掉 webstorm 灰色的数据类型提示
- 初探nodejs事件循环机制event loop
nodejs的特点 nodejs 具有事件驱动和非阻塞I/O的特点. 事件驱动是指nodejs把每一个任务当成事件来处理. 非阻塞I/O是指nodejs遇到I/O任务时,会从线程池调度单独的线程处理I ...
- 浅谈JSONP 的本质工作原理
json 是一种数据格式jsonp 是一种数据调用的方式. 你可以简单的理解为 带callback的json就是jsonp 话说我们访问一个页面的时候 需要像另一个网站获取部分信息, 这就是所谓的跨域 ...
- 2018-2019-2 网络对抗技术 20165231 Exp9 Web安全基础
实验内容 本实践的目标理解常用网络攻击技术的基本原理,做不少于7个题目,共3.5分.包括(SQL,XSS,CSRF).Webgoat实践下相关实验. 实验过程 WebGoat: Webgoat是OWA ...
- 通过phpMyAdmin优化mysql 数据库可能存在的问题
通过phpMyAdmin优化mysql 数据库可能存在的问题 文章来源:外星人来地球 欢迎关注,有问题一起学习欢迎留言.评论