zoukankan
html css js c++ java
How to download a file
Code
1
public
void
mDownload()
2
{
3
try
4
{
5
Map params
=
FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
6
String documentId
=
(String)params.get(
"
documentId
"
);
7
if
(main.isNum(documentId)
==
true
)
8
{
9
Query query
=
main.session.getNamedQuery(
"
upload.selectFileById
"
);
10
query.setParameter(
"
id
"
, documentId);
11
List list
=
query.list();
12
String userId,oldName,fileName;
13
if
(list.size()
>
0
)
14
{
15
Object obj[]
=
(Object[])list.get(
0
);
16
userId
=
String.valueOf(obj[
0
]);
17
oldName
=
String.valueOf(obj[
1
]);
18
fileName
=
String.valueOf(obj[
2
]);
19
//
20
if
(main.getFileType(oldName).equals(
"
.nothing
"
))
21
oldName
=
oldName
+
main.getFileType(fileName);
22
//
get the OS name
23
String osName
=
System.getProperty(
"
os.name
"
);
24
String storedir
=
""
;
25
if
(osName
==
null
)
26
osName
=
""
;
27
if
(osName.toLowerCase().indexOf(
"
win
"
)
!=
-
1
)
28
storedir
=
url
+
userId
+
"
\\
"
;
29
else
30
storedir
=
url2
+
userId
+
"
/
"
;
31
FacesContext ctx
=
FacesContext.getCurrentInstance();
32
ctx.responseComplete();
33
//
String contentType = "application/octet-stream;charset=utf-8";
34
String contentType
=
"
application/x-download
"
;
35
HttpServletResponse response
=
(HttpServletResponse) ctx.getExternalContext().getResponse();
36
response.setContentType(contentType);
37
StringBuffer contentDisposition
=
new
StringBuffer();
38
contentDisposition.append(
"
attachment;
"
);
39
contentDisposition.append(
"
filename=\
""
);
40
contentDisposition.append(oldName);
41
contentDisposition.append(
"
\
""
);
42
response.setHeader(
"
Content-Disposition
"
,
new
String(contentDisposition.toString().getBytes(System.getProperty(
"
file.encoding
"
)),
"
iso8859_1
"
));
43
ServletOutputStream out
=
response.getOutputStream();
44
byte
[] bytes
=
new
byte
[
0xffff
];
45
InputStream is
=
new
FileInputStream(
new
File(storedir
+
fileName));
46
int
b
=
0
;
47
while
((b
=
is.read(bytes,
0
,
0xffff
))
>
0
)
48
{
49
out.write(bytes,
0
, b);
50
}
51
is.close();
52
ctx.responseComplete();
53
}
54
}
55
}
catch
(Exception ex)
{ex.printStackTrace();}
56
}
查看全文
相关阅读:
如何抓住用户痛点做产品?
分析需求场景对产品设计的意义
【用户分析-用户场景】这TM才是产品思维!
WebUploader实现浏览器端大文件分块上传
npm 安装包报错 rollbackFailedOptional
PAT 甲级 1074 Reversing Linked List (25 分)(链表部分逆置,结合使用双端队列和栈,其实使用vector更简单呐)...
PAT 甲级 1071 Speech Patterns (25 分)(map)
P3370 【模板】字符串哈希
PageRank算法原理与Python实现
PAT-2019年冬季考试-甲级 7-4 Cartesian Tree (30分)(最小堆的中序遍历求层序遍历,递归建树bfs层序)...
原文地址:https://www.cnblogs.com/liuzhengdao/p/1580000.html
最新文章
ASP.NET replay attack detection again 解决方案
Cookie replay attacks in ASP.NET when using forms authentication
FormsAuthentication.SignOut() does not log the user out
用户体验设计遇见色彩情感
管理员 修改MySQL 5.7.9 新版本的root密码方法以及一些新变化整理
springMVC + hadoop + httpclient 文件上传请求直接写入hdfs
Hadoop HDFS文件系统通过java FileSystem 实现上传下载等
对百度WebUploader开源上传控件的二次封装,精简前端代码(两句代码搞定上传)
uploadify+批量上传文件+java
JQuery上传插件Uploadify使用详解
热门文章
Echarts使用
从一个前端项目实践 Git flow 的流程与参考
前端知识的一些总结
Linux平台Cpu使用率的计算
怎样做出优秀的扁平化设计风格 PPT 或 Keynote 幻灯片演示文稿?(装)
使用Echarts实现动态曲线图表
WEB前端介绍
15个必须知道的chrome开发者技巧
大数据时代下的用户洞察:用户画像建立(ppt版)
【PPT分享】五类常见的用户分析场景
Copyright © 2011-2022 走看看