java學習
java學習
Struts2下載文件的程序
在jsp里寫:
<
a href
=
"
download.action
"
>
點此下載文件
</
a
>
在struts.xml中:
<?
xml version
=
"
1.0
"
encoding
=
"
UTF-8
"
?>
<!
DOCTYPE struts PUBLIC
"
-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN
"
"
http://struts.apache.org/dtds/struts-2.1.7.dtd
"
>
<
struts
>
<
package
name
=
"
mydown
"
extends
=
"
struts-default
"
>
<
action name
=
"
download
"
class
=
"
com.yjw.web.DownAction
"
>
<
param name
=
"
id
"
>
1
</
param
>
<
result type
=
"
stream
"
>
<!--
下載文件的mime類型
-->
<
param name
=
"
contentType
"
>
$
{fileType}
</
param
>
<!--
下載文件的描述
-->
<
param name
=
"
contentDisposition
"
>
attachment;filename
=
$
{fileName}
</
param
>
<!--
設置緩沖區(qū)大小
-->
<
param name
=
"
bufferSize
"
>
1024
</
param
>
<!--
獲得文件名的getxxx方法的名字 ,不包含get
-->
<
param name
=
"
inputName
"
>
inputStream
</
param
>
</
result
>
</
action
>
</
package
>
</
struts
>
在action中的程序:
package
com.yjw.web;
import
java.io.File;
import
java.io.FileInputStream;
import
java.io.IOException;
import
java.io.InputStream;
import
org.apache.struts2.ServletActionContext;
import
com.opensymphony.xwork2.Action;
public
class
DownAction
implements
Action
{
private
String fileName;
private
String id;
private
String fileType;
public
String execute()
throws
Exception
{
return
"
success
"
;
}
public
InputStream getInputStream()
throws
IOException
{
String path
=
ServletActionContext.getServletContext().getRealPath(
"
/
"
);
if
(id.equals(
"
1
"
))
{
path
=
path
+
"
download/less.pdf
"
;
fileName
=
"
css.pdf
"
;
fileType
=
"
application/pdf
"
;
}
else
{
path
=
path
+
"
download/data.xlsx
"
;
fileName
=
"
data.xlsx
"
;
fileType
=
"
application/vnd.ms-excel
"
;
}
FileInputStream stream
=
new
FileInputStream(
new
File(path));
return
stream;
}
public
String getFileName()
{
return
fileName;
}
public
void
setFileName(String fileName)
{
this
.fileName
=
fileName;
}
public
String getId()
{
return
id;
}
public
void
setId(String id)
{
this
.id
=
id;
}
public
String getFileType()
{
return
fileType;
}
public
void
setFileType(String fileType)
{
this
.fileType
=
fileType;
}
}
posted on 2012-04-29 19:36
楊軍威
閱讀(193)
評論(0)
編輯
收藏
新用戶注冊
刷新評論列表
只有注冊用戶
登錄
后才能發(fā)表評論。
網(wǎng)站導航:
博客園
IT新聞
Chat2DB
C++博客
博問
管理
導航
BlogJava
首頁
新隨筆
聯(lián)系
聚合
管理
統(tǒng)計
隨筆 - 391
文章 - 34
評論 - 20
引用 - 0
常用鏈接
我的隨筆
我的評論
我的參與
最新評論
留言簿
給我留言
查看公開留言
查看私人留言
隨筆檔案
2021年9月 (1)
2021年6月 (1)
2021年4月 (1)
2021年1月 (1)
2020年12月 (1)
2020年11月 (2)
2020年10月 (1)
2020年8月 (2)
2020年7月 (2)
2020年5月 (1)
2020年4月 (7)
2020年3月 (4)
2019年11月 (1)
2019年6月 (1)
2019年2月 (1)
2018年11月 (2)
2018年8月 (1)
2018年7月 (1)
2018年6月 (1)
2018年5月 (8)
2018年4月 (3)
2018年3月 (10)
2018年2月 (5)
2018年1月 (5)
2017年12月 (12)
2017年11月 (7)
2017年9月 (13)
2017年8月 (13)
2017年7月 (10)
2017年6月 (2)
2017年3月 (3)
2017年2月 (5)
2016年9月 (2)
2016年8月 (2)
2016年7月 (3)
2016年6月 (2)
2016年5月 (4)
2016年4月 (1)
2014年12月 (6)
2013年11月 (1)
2013年9月 (4)
2013年8月 (16)
2013年7月 (6)
2013年6月 (27)
2013年5月 (21)
2013年4月 (48)
2013年3月 (61)
2013年2月 (11)
2013年1月 (33)
2012年12月 (19)
2012年11月 (8)
2012年10月 (2)
2012年9月 (2)
2012年5月 (6)
2012年4月 (4)
2011年11月 (8)
搜索
最新評論
1.?re: Form 表單域與 Java 對象屬性的自動裝配功能[未登錄]
沒排版,看著好累
--alex
2.?re: struts2實現(xiàn)登錄攔截器和驗證方法
嗯嗯
--嗯嗯
3.?re: 用struts2給的jar包解析json
阿斯頓發(fā)送到
--阿斯蒂芬
4.?re: ApplicationContextAware接口的作用
解決
--解決
5.?re: jfinal攔截器筆記5[未登錄]
2132
--123
閱讀排行榜
1.?ApplicationContextAware接口的作用(37201)
2.?svn使用(12310)
3.?git 解決 unable to get local issuer certificate 問題(10187)
4.?jfinal攔截器筆記5(8696)
5.?$.cookie的用法(8172)
評論排行榜
1.?struts2實現(xiàn)登錄攔截器和驗證方法(5)
2.?jquery阻止表單提交(3)
3.?jfinal筆記1(2)
4.?extjs和highcharts整合顯示數(shù)據(jù)曲線和打印圖片(2)
5.?XPath解析,寫入,修改xml文件(1)
Powered by:
BlogJava
Copyright © 楊軍威
主站蜘蛛池模板:
山丹县
|
屏山县
|
崇左市
|
诸暨市
|
宁乡县
|
大城县
|
遂溪县
|
阜城县
|
忻州市
|
马山县
|
芒康县
|
白朗县
|
黎平县
|
大埔县
|
昆明市
|
贵定县
|
得荣县
|
普定县
|
米林县
|
孝昌县
|
汝南县
|
嘉祥县
|
富锦市
|
阿合奇县
|
嘉义市
|
乡城县
|
开原市
|
德阳市
|
宜兴市
|
喜德县
|
安泽县
|
大厂
|
兰西县
|
新田县
|
甘肃省
|
太谷县
|
呼玛县
|
新巴尔虎左旗
|
高青县
|
罗平县
|
革吉县
|