以下是注解方式的struts2文件下载
@ResultPath("/")
@Results(value={@Result(name="success", type="stream",params={"contentType","image/jpg","inputName","targetFile","contentDisposition","attachment;filename=123.jpg","bufferSize","4096"})})
public
class DownloadAction
extends ActionSupport {
private InputStream targetFile;
public InputStream getTargetFile() {
//
123.jpg文件必须存在,特别注意
//
否则会抛异常 Can not find a java.io.InputStream with the name [targetFile]
//
上边异常的解决参见
return ServletActionContext.getServletContext().getResourceAsStream("/upload/123.jpg");
}
public
void setTargetFile(InputStream targetFile) {
this.targetFile = targetFile;
}
}
index.jsp
<%
@ page language
=
"
java
"
import
=
"
java.util.*
"
pageEncoding
=
"
UTF-8
"
%>
<%
String
path
=
request.getContextPath();
String
basePath
=
request.getScheme()
+
"
://
"
+
request.getServerName()
+
"
:
"
+
request.getServerPort()
+
path
+
"
/
"
;
%>
<!
DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
>
<
html
>
<
head
>
<
base
href
="<%=basePath%>"
>
<
title
>My JSP 'index.jsp' starting page
</
title
>
<
meta
http-equiv
="pragma"
content
="no-cache"
>
<
meta
http-equiv
="cache-control"
content
="no-cache"
>
<
meta
http-equiv
="expires"
content
="0"
>
<
meta
http-equiv
="keywords"
content
="keyword1,keyword2,keyword3"
>
<
meta
http-equiv
="description"
content
="This is my page"
>
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</
head
>
<
body
>
<
a
href
="download.action"
>123.jpg
</
a
>
</
body
>
</
html
>
struts2文件下载
原创
©著作权归作者所有:来自51CTO博客作者咔咔是咖咖的原创作品,请联系作者获取转载授权,否则将追究法律责任
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
Struts2文件下载
编写Action类 提供输入流import com.opensymphony.xwork2.ActionSupport;im
struts2 文件下载 java struts 输入流 -
文件下载 struts2文件下载 struts2注解
1、配置文件方式下载文件:这种方式,网上有很多例子,我没有试过,我用的是注解方式。2、注解方式下
文件下载 struts2文件下载 struts2注解 java 下载文件 -
struts2文件上传 下载
多文件上传的要求
文件上传 文件名 上传 -
Struts2文件上传与下载
struts2文件上传与下载(中文问题已解决)
struts string file exception upload