码迷,mamicode.com
首页 > 其他好文 > 详细

Struts2文件下载

时间:2016-04-23 19:37:40      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:

 1 package cn.temp;
 2 
 3 import java.io.FileInputStream;
 4 import java.io.InputStream;
 5 import java.net.URLEncoder;
 6 
 7 import org.apache.struts2.ServletActionContext;
 8 
 9 import com.opensymphony.xwork2.ActionContext;
10 import com.opensymphony.xwork2.ActionSupport;
11 
12 public class ThreeAction extends ActionSupport {
13     // 1: 开发execute方法,如果execute方法正常执行成功,则返回successs/down
14     // 只有返回string以后才会去下载
15     @Override
16     public String execute() throws Exception {
17         System.err.println("判断用户的积分...");
18         //对名称进行编码
19         String str = "倒霉.mp4";
20         str = URLEncoder.encode(str, "UTF-8");
21         ActionContext.getContext().put("name", str);
22         return "down";
23     }
24 
25     // 2:如果返回到了<result name="down" type="stream"/>
26     // 此时就会开始调用getInputSteam方法获取文件的二进制数据
27     public InputStream getFile() throws Exception {
28         String path = ServletActionContext.getServletContext().getRealPath("/files/dmx1.mp4");
29         InputStream in = new FileInputStream(path);
30         return in;
31     }
32 }

struts  xml文件配置

<!-- 配置下载 -->
		<action name="three" class="cn.temp.ThreeAction">
			<result name="down" type="stream">
				<param name="contentType">application/force-download</param>
				<param name="contentDisposition">attachment;filename=${name}</param>
				<!-- 指定哪一个方法可以获取二进制数据,则在类中,必须要有一个应运getFile():InputStream -->
				<param name="inputName">file</param>
			</result>
		</action>

  inputname  默认是InputStream

Struts2文件下载

标签:

原文地址:http://www.cnblogs.com/fujilong/p/5425184.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!