Struts2 文件下载
1.Struts2_Download程序结构:
2.AuthorityDownAction.java源代码:
package com.xqh.struts2.action;
import java.io.InputStream;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import java.util.Map;
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("xqh"))
{
return SUCCESS;
}
ctx.put("tip" , "您还没有登陆,或者登陆的用户名不正确,请重新登陆!");
return LOGIN;
}
}
3.FileDownloadAction源代码:
package com.xqh.struts2.action;
import java.io.InputStream;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class FileDownloadAction extends ActionSupport {
private String inputPath; // 该属性可以在配置文件中动态指定该属性值
public String getInputPath() {
return inputPath;
}
public void setInputPath(String inputPath) {
this.inputPath = inputPath;
}
/**
* 定义一个返回InputStream的方法,
* 该方法将作为被下载文件的入口,
* 且需要配置stream类型结果时指定inputName参数,
* inputName参数的值就是方法去掉get前缀、首字母小写的字符串
* @return InputStream
* @throws Exception
*/
public InputStream getTargetFile() throws Exception {
return ServletActionContext.getServletContext().getResourceAsStream(
inputPath);
}
}
4.LoginAction源代码:
package com.xqh.struts2.action;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
public class LoginAction implements Action
{
private String user;
private String pass;
public void setUser(String user)
{
this.user = user;
}
public String getUser()
{
return (this.user);
}
public void setPass(String pass)
{
this.pass = pass;
}
public String getPass()
{
return (this.pass);
}
public String execute()
{
ActionContext.getContext().getSession()
.put("user" , getUser());
return SUCCESS;
}
}
5.struts.xml配置文件:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<constant name="struts.i18n.encoding" value="GBK"></constant>
<package name="download" namespace="/" extends="struts-default">
<action name="download" class="com.xqh.struts2.action.FileDownloadAction">
<!-- 指定被下载资源的位置 -->
<param name="inputPath">
\images\Koala(考拉).jpg
</param>
<!-- 配置结果类型为stream的结果 -->
<result name="success" type="stream">
<!-- 指定下载文件的文件类型 -->
<param name="contentType">image/jpg</param>
<!-- 指定由getTargetFile()方法返回被下载的InputStream -->
<param name="inputName">targetFile</param>
<param name="contentDisposition">filename="Penguins.jpg"</param>
<!-- 指定下载文件的缓冲大小 -->
<param name="bufferSize">4096</param>
</result>
</action>
<action name="download2" class="com.xqh.struts2.action.AuthorityDownAction">
<!-- 定义被下载文件的物理资源 -->
<param name="inputPath">\images\Koala(考拉).jpg</param>
<result name="success" type="stream">
<!-- 指定下载文件的文件类型 -->
<param name="contentType">application/zip</param>
<!-- 指定由getTargetFile()方法返回被下载文件的InputStream -->
<param name="inputName">targetFile</param>
<param name="contentDisposition">filename="Penguins.zip"</param>
<!-- 指定下载文件的缓冲大小 -->
<param name="bufferSize">4096</param>
</result>
<!-- 定义一个名为login的结果 -->
<result name="login">/input.jsp</result>
</action>
<action name="login" class="com.xqh.struts2.action.LoginAction">
<result>/stuts2Down.html</result>
</action>
<action name="">
<result>.</result>
</action>
</package>
</struts>
6.web.xml配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
7.index.jsp源代码:
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
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>
<jsp:forward page="input.jsp"></jsp:forward>
</body>
</html>
8.input.jsp源代码:
<%@ page contentType="text/html;charset=GBK"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> 下载前的登陆页面 </TITLE>
</HEAD>
<BODY>
<h3>下载前的登陆页面</h3>
${requestScope.tip}
<FORM METHOD="POST" ACTION="login.action">
用户名:<INPUT TYPE="text" NAME="user"/><br>
密码:<INPUT TYPE="text" NAME="pass"/><br>
<INPUT TYPE="submit" value="登陆"/><br>
</FORM>
</BODY>
</HTML>
9.struts2Down.html源代码:
<html>
<head>
<title>Struts2的文件下载</title>
</head>
<body>
<h1>Struts2的文件下载</h1>
<ul>
<li>
下载koala图片:<a href="download.action">下载图形文件</a>
</li>
<li>
下载企鹅压缩文件:<a href="download2.action">下载压缩文件</a>
</li>
</ul>
</body>
</html>
10.lib目录下的jar包:
11.程序运行结果截图:
登陆界面:
下载界面: