struts 的文件下载;

在这里主要在之前的上传功能的基础上,总结下下载功能的实现,所以配置struts2的步骤就不重复说了。

前台下载的代码:

<a href="download.action?filename=/文件.txt">文件下载</a>

这里其实就是个a标签;

后台的action代码:

package com.yxq.action;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class FileDowloadaction extends ActionSupport {
    private String inputPath;
    private String filename;
    private String DowloadFileName;
    
    public String getDowloadFileName() {
        String downloadFileName="";
        try {
            downloadFileName = URLEncoder.encode(filename,"UTF-8");
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        return downloadFileName;
    }

    public void setDowloadFileName(String dowloadFileName) {
        DowloadFileName = dowloadFileName;
    }

    public String getFilename() {
        return filename;
    }

    public void setFilename(String filename) {
        this.filename = filename;
    }

    public String getInputPath() {
        return inputPath;
    }

    public void setInputPath(String inputPath) {
        this.inputPath = inputPath;
    }
    public InputStream getInputStream(){
        
        
        String path = ServletActionContext.getServletContext().getRealPath("/userfiles/images");
        String filepath = path+"\\"+filename;
        String[] s =filepath.split("\\\\"); 
        
        System.out.println(s[7]);
        File file = new File(filepath);
        try {
            return FileUtils.openInputStream(file);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            
            e.printStackTrace();
            return null;
        }
        
        //return ServletActionContext.getServletContext().getResourceAsStream(inputPath);            
    }
    
    public String execute() throws Exception{
        
        return SUCCESS;
    }
    
    
}
View Code

stuts.xml的配置;

<action name="download" class="com.yxq.action.FileDowloadaction">
               <param name="inputPath">/userfiles/images/2.jpg</param>
               <result name="success" type="stream">
                   <param name="contentType">application/octet-stream</param>
                   <param name="inputName">inputStream</param>
                   <param name="contentDisposition">attachment;filename="${DowloadFileName}"</param>
                   <param name="bufferSize">8192</param>
               </result>
           </action>
View Code

后台代码的讲解:

struts2的过滤器会先拦截用户的请求,得到用户需要的action,然后通过配置文件的action默认的拦截器;并生成对应的拦截类的对象。也就是我们写的action类的对象。以及还会产生实现了ActionInvocation类的对象;并通过ActionInvocation来控制action 对象;即调用ActionInvocation.invoke()方法。然后递归调用拦截器;如果拦截器没有问题,就会调用action类的execute()方法,然后filter发现该action对应的返回类型type是stream,而默认的是dispacher。filter会产生一个StreamResult类的对象,对stream类型进行处理,进一步通过result中的相关参数inputName,contentDisposition,contentType等等,以及通过调用action中的getinputStream()方法来返回下载文件需要的流。

posted @ 2015-08-24 10:27  坏婷坏婷  阅读(163)  评论(0编辑  收藏  举报