Struts2 文件下载
1 package lee; 2 3 import java.io.InputStream; 4 import org.apache.struts2.ServletActionContext; 5 import com.opensymphony.xwork2.Action; 6 import com.opensymphony.xwork2.ActionContext; 7 import java.util.Map; 8 /** 9 * Description: 10 * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a> 11 * <br/>Copyright (C), 2001-2010, Yeeku.H.Lee 12 * <br/>This program is protected by copyright laws. 13 * <br/>Program Name: 14 * <br/>Date: 15 * @author Yeeku.H.Lee kongyeeku@163.com 16 * @version 1.0 17 */ 18 public class AuthorityDownAction implements Action 19 { 20 private String inputPath; 21 public void setInputPath(String value) 22 { 23 inputPath = value; 24 } 25 public InputStream getTargetFile() throws Exception 26 { 27 //ServletContext提供getResourceAsStream()方法 28 //返回指定文件对应的输入流 29 return ServletActionContext.getServletContext() 30 .getResourceAsStream(inputPath); 31 } 32 public String execute() throws Exception 33 { 34 //取得ActionContext实例 35 ActionContext ctx = ActionContext.getContext(); 36 //通过ActionContext访问用户的HttpSession 37 Map session = ctx.getSession(); 38 String user = (String)session.get("user"); 39 //判断Session里的user是否通过检查 40 if ( user != null && user.equals("crazyit")) 41 { 42 return SUCCESS; 43 } 44 ctx.put("tip" , 45 "您还没有登录,或者登录的用户名不正确,请重新登录!"); 46 return LOGIN; 47 } 48 }
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 2 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 3 <html xmlns="http://www.w3.org/1999/xhtml"> 4 <head> 5 <title>Struts 2的文件下载</title> 6 <meta name="author" content="Yeeku.H.Lee" /> 7 <meta name="website" content="http://www.crazyit.org" /> 8 <meta http-equiv="Content-Type" content="text/html; charset=GBK" /> 9 </head> 10 <body> 11 <h3>Struts 2的文件下载</h3> 12 <ul> 13 <li>下载疯狂Java联盟的Logo: 14 <a href="download.action">下载图形文件</a></li> 15 <li>下载疯狂Java联盟的Logo的压缩文件: 16 <a href="download2.action">下载压缩文件</a></li> 17 </ul> 18 </body> 19 </html>
1 <?xml version="1.0" encoding="GBK"?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" 4 "http://struts.apache.org/dtds/struts-2.1.dtd"> 5 <struts> 6 7 <constant name="struts.custom.i18n.resources" 8 value="globalMessages"/> 9 <constant name="struts.i18n.encoding" value="GBK"/> 10 11 <package name="lee" extends="struts-default"> 12 13 <action name="download" class="lee.FileDownloadAction"> 14 <!-- 指定被下载资源的位置 --> 15 <param name="inputPath">\images\疯狂Java联盟.gif</param> 16 <!-- 配置结果类型为stream的结果 --> 17 <result name="success" type="stream"> 18 <!-- 指定下载文件的文件类型 --> 19 <param name="contentType">image/gif</param> 20 <!-- 指定由getTargetFile()方法返回被下载文件的InputStream --> 21 <param name="inputName">targetFile</param> 22 <param name="contentDisposition">filename="crazyit.gif"</param> 23 <!-- 指定下载文件的缓冲大小 --> 24 <param name="bufferSize">4096</param> 25 </result> 26 </action> 27 28 <action name="download2" class="lee.AuthorityDownAction"> 29 <!-- 定义被下载文件的物理资源 --> 30 <param name="inputPath">\images\crazyit.zip</param> 31 <result name="success" type="stream"> 32 <!-- 指定下载文件的文件类型 --> 33 <param name="contentType">application/zip</param> 34 <!-- 指定由getTargetFile()方法返回被下载文件的InputStream --> 35 <param name="inputName">targetFile</param> 36 <param name="contentDisposition">filename="crazyit.zip"</param> 37 <!-- 指定下载文件的缓冲大小 --> 38 <param name="bufferSize">4096</param> 39 </result> 40 <!-- 定义一个名为login的结果 --> 41 <result name="login">/input.jsp</result> 42 </action> 43 44 <action name="login" class="lee.LoginAction"> 45 <result>/stuts2Down.html</result> 46 </action> 47 <action name=""> 48 <result>.</result> 49 </action> 50 51 </package> 52 </struts>