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
}
查看全文
相关阅读:
獲得當年的周別
動態更改GridView 的Page 頁碼的Style
發送Mail(帶Table)
C# DataSet和DataTable详解
利用JS动态创建html控件并在后台实现取值
ArrayList与string、string[]的转换代码
js操作listbox的方法
ASP.NET中Get和Post的用法 Request.QueryString,Request.Form,Request.Params的区别 [转]
window.showModalDialog关闭子页面刷新父页面
FTPClient upload & download class
原文地址:https://www.cnblogs.com/liuzhengdao/p/1580000.html
最新文章
JavaP: 2_3,类成员具有的控制修饰符
JavaP: 2_2、域,域初始化,静态域(Static field),Final域
plsql自定义设置 自动提示
oracle本地服务名配置说明
工具善其事,必先被苦逼的其器所钝伤然后打磨之才能利其器
js || 运算符 config = config || {};
ExtJS入门之事件(fireEvent)
【技术贴】ActiveX 部件不能创建对象 Wscript.Shell,右键打开所在目录的批处理ba
《钢的琴》观后感
【技术贴】ActiveX 部件不能创建对象 Wscript.Shell,右键打开所在目录的批处理ba
热门文章
如何让div水平居中以及垂直居中,在宽高不定的情况下
关于DataTable的Compute使用
iis7.0 发布MVC
根据索引获取转换枚举值,根据枚举值转换获取索引
转网站发布“试图加载不同的格式”解决
转—DataTable按时间排序和查询
转:"DataTable已属于另一个数据集"[
(转)ibatis实现Iterate的使用
c# 获取当前季的第一天以及最后一天
複雜表頭
Copyright © 2011-2022 走看看