struts2:下载文件

文件下载

 

页面:

<a href="${pageContext.request.contextPath }/file/downLoadAction.action?relativePath=${relativePath }">${fileFileName}</a>

 

struts.xml

         这里主要注意的是,使用的result的类型是stream

<action name="downLoadAction" class="hwt.action.DownLoadAction">
    <!- - 设置result的类型为stream- - >
            <result name="downLoad"type="stream">
                <param name="contentType">text/plain</param>
                <param name="inputName">fileStream</param>
                <param
name="contentDisposition">filename="${fileName}"</param>
                <param name="bufferSize">1024</param>
            </result>
        </action>


 

Action中        

         继承actionsupport

public class DownLoadAction extends ActionSupport{
    private String fileName;
    private String relativePath ;
   
    /*
     * getFileStream()这个方法的名字和struts.xml的配置文件中的
     * <paramname="inputName">fileStream</param>
     */
    public InputStream getFileStream(){
        relativePath = relativePath.replace("\\","/");
        return ServletActionContext.getServletContext().getResourceAsStream("/"+FileUploadUtils.baseName+"/"+relativePath);
    }
   
    public String execute(){
        return "downLoad";
    }
 
    public String getFileName() {
        fileName = relativePath.substring(relativePath.lastIndexOf("/")+1);
        return fileName;
    }
 
    public void setFileName(String fileName) {
        this.fileName = fileName;
    }
 
    public String getRelativePath() {
        return relativePath;
    }
 
    public void setRelativePath(String relativePath) {
        this.relativePath = relativePath;
    }
}


你可能感兴趣的:(struts2:下载文件)