下载文件
来源:互联网 发布:关键字排名优化软件 编辑:程序博客网 时间:2024/06/05 20:28
/**
* downloadFile:(下载文件). <br/>
*
* @author wpengfei
* @param inv
* @param path 全路径
* @param fileOriginName 文件原名称
* @since JDK 1.6
*/
private void downloadFile(Invocation inv,
String path, String fileOriginName){
String userAgent = inv.getRequest().getHeader("User-Agent");
//针对IE或者以IE为内核的浏览器:
if (userAgent.contains("MSIE")||userAgent.contains("Trident")) {
try {
fileOriginName = URLEncoder.encode(fileOriginName, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
} else {
//非IE浏览器的处理:
try {
fileOriginName = new String(fileOriginName.getBytes("UTF-8"),"ISO-8859-1");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
InputStream ins = null;
BufferedInputStream bins = null;
OutputStream outs = null;
BufferedOutputStream bouts = null;
try {
path = new String(path.getBytes("ISO-8859-1"), "UTF-8");
File file = new File(path);// 构造要下载的文件
if (file.exists()) {
// String suffix = path.substring(path.lastIndexOf(".") + 1);
ins = new FileInputStream(path);// 构造一个读取文件的IO流对象
bins = new BufferedInputStream(ins);// 放到缓冲流里面
outs = inv.getResponse().getOutputStream();// 获取文件输出IO流
bouts = new BufferedOutputStream(outs);
inv.getResponse().setContentType("application/octet-stream;charset=utf-8");// 设置response内容的类型
inv.getResponse().setHeader("Content-disposition", String.format("attachment; filename=\"%s\"", fileOriginName));
inv.getResponse().setCharacterEncoding("UTF-8");
int bytesRead = 0;
byte[] buffer = new byte[8192];
// 开始向网络传输文件流
while ((bytesRead = bins.read(buffer, 0, 8192)) != -1) {
bouts.write(buffer, 0, bytesRead);
}
bouts.flush();// 这里一定要调用flush()方法
} else {
logger.info("==============>>下载的文件不存在");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (ins != null) {
ins.close();
ins = null;
}
if (bins != null) {
bins.close();
bins = null;
}
if (outs != null) {
outs.close();
outs = null;
}
if (bouts != null) {
bouts.close();
bouts = null;
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
* downloadFile:(下载文件). <br/>
*
* @author wpengfei
* @param inv
* @param path 全路径
* @param fileOriginName 文件原名称
* @since JDK 1.6
*/
private void downloadFile(Invocation inv,
String path, String fileOriginName){
String userAgent = inv.getRequest().getHeader("User-Agent");
//针对IE或者以IE为内核的浏览器:
if (userAgent.contains("MSIE")||userAgent.contains("Trident")) {
try {
fileOriginName = URLEncoder.encode(fileOriginName, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
} else {
//非IE浏览器的处理:
try {
fileOriginName = new String(fileOriginName.getBytes("UTF-8"),"ISO-8859-1");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
InputStream ins = null;
BufferedInputStream bins = null;
OutputStream outs = null;
BufferedOutputStream bouts = null;
try {
path = new String(path.getBytes("ISO-8859-1"), "UTF-8");
File file = new File(path);// 构造要下载的文件
if (file.exists()) {
// String suffix = path.substring(path.lastIndexOf(".") + 1);
ins = new FileInputStream(path);// 构造一个读取文件的IO流对象
bins = new BufferedInputStream(ins);// 放到缓冲流里面
outs = inv.getResponse().getOutputStream();// 获取文件输出IO流
bouts = new BufferedOutputStream(outs);
inv.getResponse().setContentType("application/octet-stream;charset=utf-8");// 设置response内容的类型
inv.getResponse().setHeader("Content-disposition", String.format("attachment; filename=\"%s\"", fileOriginName));
inv.getResponse().setCharacterEncoding("UTF-8");
int bytesRead = 0;
byte[] buffer = new byte[8192];
// 开始向网络传输文件流
while ((bytesRead = bins.read(buffer, 0, 8192)) != -1) {
bouts.write(buffer, 0, bytesRead);
}
bouts.flush();// 这里一定要调用flush()方法
} else {
logger.info("==============>>下载的文件不存在");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (ins != null) {
ins.close();
ins = null;
}
if (bins != null) {
bins.close();
bins = null;
}
if (outs != null) {
outs.close();
outs = null;
}
if (bouts != null) {
bouts.close();
bouts = null;
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
0 0
- 下载图片,下载文件
- 文件下载
- 文件下载
- 文件下载
- 文件下载
- 文件下载
- 文件下载
- 下载文件
- 文件下载
- 下载文件
- 文件下载
- 文件下载
- 下载文件
- 文件下载
- 下载文件
- 下载文件
- 文件下载
- 下载文件
- 如何在编译器(vs2013)里设置调用别人写好的lib和dll步骤
- SAP Business One与国内软件厂商产品对比
- android下的代码混淆
- 葛洲坝电力集团责任有限公司改变企业新句号
- 文章标题
- 下载文件
- jQuery学习教程十一: jQuery
- Activiti-Quick Start Guide- ActivitiDeveloperQuickStart 下载导入就可以运行
- 一个单独的条形码扫描功能实现
- vue教程集
- python pip 手动安装
- java Data、String、Long三种日期类型之间的相互转换
- BJ模拟 Goodness(凸包+三分答案)
- 使用Groovy操作文件