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
}
查看全文
相关阅读:
第72天: PySpider框架的使用
第71天: Python Scrapy 项目实战
Web前端资源汇总
1201即将到来
C#自定义事件模拟风吹草摇摆
HTML5 Canvas爱心时钟代码
CSS3圆环动态弹出菜单
CSS3实现Loading动画特效
HTML5优势
CSS3扁平化Loading动画特效
原文地址:https://www.cnblogs.com/liuzhengdao/p/1580000.html
最新文章
第93天:文件读写
第91天:Python matplotlib introduction
第90天:NumPy 位运算与算术函数
第89天:NumPy 排序和筛选函数
第88天: OAuth2.0 客户端实战
第87天: OAuth2.0 简介
第86天:Python SQLAlchemy
第85天:NumPy 统计函数
第84天:NumPy 数学函数
第83天:NumPy 字符串操作
热门文章
第82天: JWT 简介
第81天:NumPy Ndarray_Object&NumPy_Data_Type
第80天:Python-Operation_MySQL
第79天:数据分析之 Numpy 初步
第78天: Python 操作 MongoDB 数据库介绍
第77天:Python 操作 SQLite
第76天:Scrapy 模拟登陆
第75天: Python 操作 Redis 数据库介绍
第74天:Python newspaper 框架
第73天: itchat 微信机器人简介
Copyright © 2011-2022 走看看