我的漫漫程序之旅
專注于JavaWeb開發(fā)
隨筆 - 39, 文章 - 310, 評論 - 411, 引用 - 0
數(shù)據(jù)加載中……
JSP實現(xiàn)點擊鏈接后下載文件(相當于右鍵另存)功能
/** */
/**
* 實現(xiàn)文件另存功能
*
*
@param
text
* 文件內容
*
@param
fileName
* 文件名稱
*
@return
*/
protected
String renderFile(String text, String fileName)
throws
IOException
{
response.addHeader(
"
Content-Disposition
"
,
"
attachment; filename=
"
+
fileName);
response.setContentType(
"
application/octet-stream
"
);
response.setCharacterEncoding(
"
GB2312
"
);
response.getWriter().write(text);
response.flushBuffer();
response.getWriter().close();
return
null
;
}
下載的action:
/** */
/**
* 提供下載的方法
*
@return
*/
public
String down()
{
String dir
=
getFullPath()
+
"
/upload/file/
"
;
try
{
if
(
!
FileUtils.exists(dir))
{
new
File(dir).mkdirs();
}
Random r
=
new
Random(System.currentTimeMillis());
Integer randomInt
=
r.nextInt();
this
.renderFile(
"
test content:
"
+
randomInt,randomInt
+
"
.txt
"
);
}
catch
(IOException e)
{
e.printStackTrace();
this
.renderText(e.getMessage());
}
return
null
;
}
頁面鏈接調用:
<
a
href
="${ctx}/va/va!down.do"
>
下載
</
a
>
posted on 2009-07-01 15:59
々上善若水々
閱讀(3032)
評論(1)
編輯
收藏
評論
#
re: JSP實現(xiàn)點擊鏈接后下載文件(相當于右鍵另存)功能
回復
更多評論
原來還有這么清晰的方法啊
2009-07-02 22:03 |
blackbat
新用戶注冊
刷新評論列表
只有注冊用戶
登錄
后才能發(fā)表評論。
網(wǎng)站導航:
博客園
IT新聞
Chat2DB
C++博客
博問
管理
Powered by:
BlogJava
Copyright © 々上善若水々
導航
BlogJava
首頁
新隨筆
聯(lián)系
管理
<
2009年7月
>
日
一
二
三
四
五
六
28
29
30
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
1
2
3
4
5
6
7
8
常用鏈接
我的隨筆
我的評論
我的參與
最新評論
留言簿
(39)
給我留言
查看公開留言
查看私人留言
隨筆檔案
(43)
2021年12月 (1)
2012年12月 (1)
2012年11月 (1)
2009年11月 (2)
2009年10月 (1)
2009年8月 (1)
2009年7月 (1)
2009年5月 (2)
2009年4月 (2)
2009年3月 (1)
2009年2月 (3)
2009年1月 (1)
2008年12月 (2)
2008年11月 (3)
2008年9月 (2)
2008年8月 (1)
2008年7月 (2)
2008年6月 (2)
2008年5月 (9)
2008年4月 (2)
2007年12月 (3)
文章分類
(304)
AJAX(7)
(rss)
flex3(1)
(rss)
Hibernate(4)
(rss)
J2EE
(rss)
J2ME(2)
(rss)
J2SE(40)
(rss)
JavaScript(59)
(rss)
JavaWeb(27)
(rss)
Java筆試與面試(52)
(rss)
JQuery(1)
(rss)
Maven(2)
(rss)
opensource(15)
(rss)
Spring(10)
(rss)
SSH整合系列(2)
(rss)
Struts1.x(3)
(rss)
Struts2(13)
(rss)
WebService(14)
(rss)
數(shù)據(jù)庫(18)
(rss)
數(shù)據(jù)結構與算法(7)
(rss)
設計模式(17)
(rss)
軟件測試(10)
(rss)
文章檔案
(257)
2021年12月 (1)
2012年12月 (2)
2012年9月 (2)
2012年7月 (5)
2012年1月 (1)
2011年10月 (1)
2010年12月 (1)
2009年9月 (2)
2009年7月 (2)
2009年6月 (1)
2009年4月 (3)
2009年3月 (3)
2009年1月 (2)
2008年12月 (4)
2008年11月 (4)
2008年10月 (2)
2008年9月 (2)
2008年8月 (10)
2008年7月 (22)
2008年6月 (13)
2008年5月 (45)
2008年4月 (17)
2008年3月 (5)
2008年2月 (11)
2008年1月 (27)
2007年12月 (45)
2007年11月 (24)
搜索
最新評論
1.?re: 使用MyEclipse構建MAVEN項目
評論內容較長,點擊標題查看
--龍平
2.?re: JDK中的URLConnection參數(shù)詳解[未登錄]
好文要頂
--wy
3.?re: Ant path 匹配原則[未登錄]
我喜歡你惠茹
--aaa
4.?re: Ant path 匹配原則[未登錄]
dfdfdfdfd
--aaa
5.?re: 利用jSMSEngine開源包進行短信的收發(fā)[未登錄]
qq郵箱
550725115@qq.com
--我是菜鳥
閱讀排行榜
1.?Linux上安裝JDK1.7與Tomcat7.0(24268)
2.?將java應用程序打包成獨立運行的.exe方法(19834)
3.?HttpURLConnection請求數(shù)據(jù)流的寫入(write)和讀取(read)(19098)
4.?基于jsTree的無限級樹JSON數(shù)據(jù)的轉換(17633)
5.?javascript文件夾選擇框的兩種解決方案(16936)
評論排行榜
1.?基于jsTree的無限級樹JSON數(shù)據(jù)的轉換(27)
2.?Flash圖表(FusionChartsV3)的簡單應用 (22)
3.?WEB頁面導出為Word文檔后分頁&橫向打印的方法 (14)
4.?javascript文件夾選擇框的兩種解決方案(10)
5.?[原創(chuàng)]J2ME/J2EE實現(xiàn)用戶登錄交互(9)
主站蜘蛛池模板:
应城市
|
长岭县
|
和平县
|
新龙县
|
邹城市
|
新丰县
|
金平
|
宁强县
|
石阡县
|
景东
|
遂川县
|
通山县
|
武威市
|
右玉县
|
长宁区
|
青州市
|
新巴尔虎右旗
|
宽甸
|
江华
|
南雄市
|
喀喇沁旗
|
浠水县
|
丘北县
|
宜阳县
|
富阳市
|
江城
|
天柱县
|
和顺县
|
北碚区
|
灵石县
|
巫山县
|
平安县
|
来安县
|
甘肃省
|
同江市
|
沂南县
|
中山市
|
武乡县
|
阿拉善左旗
|
秦皇岛市
|
七台河市
|