《struts2权威指南》学习笔记之struts2之文件下载

来源:互联网 发布:西门子软件授权 编辑:程序博客网 时间:2024/05/02 03:09

对于服务器上的英文名的文件,可以方便的使用<a href=""/></a>进行下载,但如果是中文文件名的文件,如果使用<a href="中.jpg"></a> 就会出现连接上的乱码(如%4e%54%79),为了解决这个问题,struts2通过一个文件下载拦截器支持中文命名文件的下载

首先编写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"
>
    
<filter>
      
<filter-name>struts2</filter-name>
      
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
    
</filter>
    
<filter-mapping>
      
<filter-name>struts2</filter-name>
      
<url-pattern>/*</url-pattern>
    
</filter-mapping>
 
 
    
<filter>
      
<filter-name>struts-cleanup</filter-name>
      
<filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>
    
</filter>
    
<filter-mapping>
      
<filter-name>struts-cleanup</filter-name>
      
<url-pattern>/*</url-pattern>
    
</filter-mapping>
</web-app>

 文件下载Action

struts2的文件下载Action与普通的Action并没有太大不同,仅仅是该action需要提供一个返回InputStream流的方法
,该输入流代表了被下载文件的入口,该Action类代码如下:

package lee;

import java.io.InputStream;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.Action;


public class FileDownloadAction implements Action 
{

    
private String inputPath;
    
public void setInputPath(String value)
    
{
        inputPath 
= value;
    }


    
/*
     下载用的Action应该返回一个InputStream实例,
     该方法对应在result里的inputName属性值为targetFile
    
*/

    
public InputStream getTargetFile() throws Exception 
    
{
        
return ServletActionContext.getServletContext().getResourceAsStream(inputPath);
    }


    
public String execute() throws Exception
    
{
        
return SUCCESS;
    }


}

配置Action

contentType:指定被下载文件的文件类型
inputName:指定被下载文件的入口输入流
contentDisposition:指定下载文件的文件名
bufferSize:指定下载文件时的缓冲大小

因为stream结果类型的逻辑视图是返回给客户端一个输入流,因此无需指定location属性

 

<?xml version="1.0" encoding="GBK"?>

<!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.custom.i18n.resources" value="globalMessages"/>
    
<constant name="struts.i18n.encoding" value="GBK"/>
    
<package name="lee" extends="struts-default">

        
<default-action-ref name="download"/>

        
<action name="download" class="lee.FileDownloadAction">
            
<param name="inputPath">images中.gif</param>
                
<result name="success" type="stream">
                
<param name="contentType">image/gif</param>
                
<param name="inputName">targetFile</param>
                
<param name="contentDisposition">filename="struts.gif"</param>
                
<param name="bufferSize">4096</param>
            
</result>
        
</action>

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

    
</package>
</struts>

登陆Action:

 

package lee;

import java.io.InputStream;
import org.apache.struts2.ServletActionContext;
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;
    }

}

 

资源文件:globalMessages.properties 内容为空

登陆页面: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>

下载页面:struts2Down.html

 

<html>
<head>
    
<title>Struts2的文件下载</title>
</head>

<body>
    
<h1>Struts2的文件下载</h1>

    
<ul>
    
<li>
        下载Struts2的Logo:
<href="download.action">下载图形文件</a> 
    
</li>
    
<li>
        下载Struts2的Logo的压缩文件:
<href="download2.action">下载压缩文件</a>          
    
</li>
    
</ul>
</body>
</html>

在images文件夹下copy一个图片命名为中.gif,运行input.jsp. 随便输入用户名密码登陆后即可下载struts2的logo

下面说说下载的权限控制,我们希望只有当scott用户登陆时候,才可以下载struts2的logo压缩文件,该怎样处理呢?

下面就是我们的权限控制下载Action

 

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;

public class AuthorityDownAction implements Action 
{
    
private String inputPath;
    
public void setInputPath(String value)
    
{
        inputPath 
= value;
    }


    
/*
     下载用的Action应该返回一个InputStream实例,
     该方法对应在result里的inputName属性值为targetFile
    
*/

    
public InputStream getTargetFile() throws Exception 
    
{
        
return ServletActionContext.getServletContext().getResourceAsStream(inputPath);
    }


    
public String execute() throws Exception
    
{
        ActionContext ctx 
= ActionContext.getContext();
        Map session 
= ctx.getSession();
        String user 
= (String)session.get("user");
        
if ( user !=  null && user.equals("scott"))
        
{
            
return SUCCESS;
        }

        ctx.put(
"tip" , "您还没有登陆,或者登陆的用户名不正确,请重新登陆!");
        
return LOGIN;
    }


}

 

修改struts.xml

 

<?xml version="1.0" encoding="GBK"?>

<!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.custom.i18n.resources" value="globalMessages"/>
    
<constant name="struts.i18n.encoding" value="GBK"/>
    
<package name="lee" extends="struts-default">

        
<default-action-ref name="download"/>

        
<action name="download" class="lee.FileDownloadAction">
            
<param name="inputPath">images中.gif</param>
            
<result name="success" type="stream">
                
<param name="contentType">image/gif</param>
                
<param name="inputName">targetFile</param>
                
<param name="contentDisposition">filename="struts.gif"</param>
                
<param name="bufferSize">4096</param>
            
</result>
        
</action>

        
<action name="download2" class="lee.AuthorityDownAction">
            
<param name="inputPath">imagesstruts-gif.zip</param>
            
<result name="success" type="stream">
                
<param name="contentType">application/zip</param>
                
<param name="inputName">targetFile</param>
                
<param name="contentDisposition">filename="struts-gif.zip"</param>
                
<param name="bufferSize">4096</param>
            
</result>
            
<result name="login">/input.jsp</result>

        
</action>

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

    
</package>
</struts>

 

用scott登陆,就可以下载压缩文件了

原创粉丝点击