您的位置:首页 > 编程语言 > Java开发

Struts2 文件下载

2015-07-11 15:53 253 查看
package lee;

import java.io.InputStream;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import java.util.Map;
/**
* Description:
* <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
* <br/>Copyright (C), 2001-2010, Yeeku.H.Lee
* <br/>This program is protected by copyright laws.
* <br/>Program Name:
* <br/>Date:
* @author  Yeeku.H.Lee kongyeeku@163.com
* @version  1.0
*/
public class AuthorityDownAction implements Action
{
private String inputPath;
public void setInputPath(String value)
{
inputPath = value;
}
public InputStream getTargetFile() throws Exception
{
//ServletContext提供getResourceAsStream()方法
//返回指定文件对应的输入流
return ServletActionContext.getServletContext()
.getResourceAsStream(inputPath);
}
public String execute() throws Exception
{
//取得ActionContext实例
ActionContext ctx = ActionContext.getContext();
//通过ActionContext访问用户的HttpSession
Map session = ctx.getSession();
String user = (String)session.get("user");
//判断Session里的user是否通过检查
if ( user !=  null && user.equals("crazyit"))
{
return SUCCESS;
}
ctx.put("tip" ,
"您还没有登录,或者登录的用户名不正确,请重新登录!");
return LOGIN;
}
}


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Struts 2的文件下载</title>
<meta name="author" content="Yeeku.H.Lee" />
<meta name="website" content="http://www.crazyit.org" />
<meta http-equiv="Content-Type" content="text/html; charset=GBK" />
</head>
<body>
<h3>Struts 2的文件下载</h3>
<ul>
<li>下载疯狂Java联盟的Logo:
<a href="download.action">下载图形文件</a></li>
<li>下载疯狂Java联盟的Logo的压缩文件:
<a href="download2.action">下载压缩文件</a></li>
</ul>
</body>
</html>


<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
"http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>

<constant name="struts.custom.i18n.resources"
value="globalMessages"/>
<constant name="struts.i18n.encoding" value="GBK"/>

<package name="lee" extends="struts-default">

<action name="download" class="lee.FileDownloadAction">
<!-- 指定被下载资源的位置 -->
<param name="inputPath">\images\疯狂Java联盟.gif</param>
<!-- 配置结果类型为stream的结果 -->
<result name="success" type="stream">
<!-- 指定下载文件的文件类型 -->
<param name="contentType">image/gif</param>
<!-- 指定由getTargetFile()方法返回被下载文件的InputStream -->
<param name="inputName">targetFile</param>
<param name="contentDisposition">filename="crazyit.gif"</param>
<!-- 指定下载文件的缓冲大小 -->
<param name="bufferSize">4096</param>
</result>
</action>

<action name="download2" class="lee.AuthorityDownAction">
<!-- 定义被下载文件的物理资源 -->
<param name="inputPath">\images\crazyit.zip</param>
<result name="success" type="stream">
<!-- 指定下载文件的文件类型 -->
<param name="contentType">application/zip</param>
<!-- 指定由getTargetFile()方法返回被下载文件的InputStream -->
<param name="inputName">targetFile</param>
<param name="contentDisposition">filename="crazyit.zip"</param>
<!-- 指定下载文件的缓冲大小 -->
<param name="bufferSize">4096</param>
</result>
<!-- 定义一个名为login的结果 -->
<result name="login">/input.jsp</result>
</action>

<action name="login" class="lee.LoginAction">
<result>/stuts2Down.html</result>
</action>
<action name="">
<result>.</result>
</action>

</package>
</struts>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: