Struts 2 实现文件下载功能

用struts 2 实现文件下载功能,主要是3个部分代码的编写:

1. Web前端传入文件名参数;

2. 编写寻找文件并返回InputStream的Action;

3. 在struts.xml中进行配置。

 本文将给出一个对上述二、三部分代码编写的一个例子。

 

Action:

复制代码
代码

package  a.b.c;

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

public   class  DownloadAction  extends  ActionSupport {

    
private   static   final   long  serialVersionUID  =   1656950476675155655L ;
    
private  String md5;
    
private  String filename;
    
private  String directory;

    @Override
    
public  String execute()  throws  Exception {
        
// 这里进行参数验证,保证请求的下载文件合法性
        
//  本例对传入参数md5进行验证并将其值赋予filename
         if  (md5.matches( " [\\w&&[^_]]{32}? " )) {
            filename 
=  md5.toLowerCase();
            
return  SUCCESS;
        }
        
return  INPUT;
    }

    
public  InputStream getInputStream()  throws  Exception {
        String dir 
=  directory  +  filename  +   " .txt " ;
        
return   new  FileInputStream(dir);     // 如果dir是绝对路径
//         return ServletActionContext.getServletContext().getResourceAsStream(dir);     // 如果dir是Resource下的相对路径
    }

    
/**
     * 
@return  the md5
     
*/
    
public  String getMd5() {
        
return  md5;
    }

    
/**
     * 
@param  md5
     *            the md5 to set
     
*/
    
public   void  setMd5(String md5) {
        
this .md5  =  md5;
    }

    
/**
     * 
@return  the filename
     
*/
    
public  String getFilename() {
        
return  filename;
    }

    
/**
     * 
@param  filename
     *            the filename to set
     
*/
    
public   void  setFilename(String filename) {
        
this .filename  =   this .getMd5();
    }

    
/**
     * 
@param  directory
     *            the directory to set
     
*/
    
public   void  setDirectory(String directory) {
        
this .directory  =  directory;
    }

}
复制代码

 

 

Action中没有给出directory的get方法,其目的为保护文件路径不被访问到。

execute()方法对传入参数进行验证,若验证成功,返回success,否则返回input。

getInputStream()方法将返回一个InputStream,在struts.xml中,配置了一个参数inputStream,通过这个方法获得流对象。

 

struts.xml:

 代码

复制代码
< action  name ="download"  class ="a.b.c.DownloadAction"   >
    
< param  name ="directory" > D:/download/ </ param >
    
< result  name ="success"  type ="stream" >
        
<!--  指定下载文件的内容类型,text/plain是默认类型  -->
        
< param  name ="contentType" > text/plain </ param >
        
<!--  inputName默认值是inputStream,如果action中用于读取下载文件内容的属性名是inputStream,那么可以省略这个参数  -->
        
< param  name ="inputName" > inputStream </ param >
        
<!-- 动态获取文件名,从Action中的取得filename -->
        
< param  name ="contentDisposition" >
            attachment;filename="${filename}"
        
</ param >
        
< param  name ="bufferSize" > 2048 </ param >
    
</ result >
    
< result  name ="input" > /WEB-INF/pages/inputError.jsp </ result >
</ action >
复制代码

 

 参考API SteamResult(http://struts.apache.org/2.0.14/struts2-core/apidocs/org/apache/struts2/dispatcher/StreamResult.html),其中

contentType是内容类型,根据不同文件类型参照MIME标准设置,本例是文本。

contentDisposition是文件下载的处理方式,包括内联(inline)和附件(attachment),默认是inline, 使用附件时这样配置:attachment;filename="文件名" 。

 

若Action中验证失败,转到inputError页面。 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值