java-下载资源

来源:互联网 发布:线性时不变系统 知乎 编辑:程序博客网 时间:2024/05/16 03:03
1.用到的jar包:struts2-sunspoter-stream-1.0.jar

2.struts配置:

<!-- 文件下载 -->

   <package name="default" extends="struts-default">
         <result-types>
            <result-type name="streamX" class="com.sunspoter.lib.web.struts2.dispatcher.StreamResultX" />
      </result-types>
      <action name="download" class="com.pinzhi.action.common.DownloadAction">
              <!-- 返回文件流 stream  -->
            <result name="success" type="streamX">
                <!-- 返回文件的类型,默认为 text/plain -->
                <param name="contentType">application/octet-stream;charset=ISO8859-1</param>  
                <!-- 设置下载文件的文件名 -->
                <param name="contentDisposition">attachment;filename="${fileName}"</param>
                 <!--输入流名称,对应Action类中的getDownloadFile()方法  -->
                <param name="inputName">downloadFile</param>
                 <!-- 设定下载文件时缓冲区的大小 -->
                <param name="bufferSize">4096</param>
            </result>
            <!-- 返回下载错误页面 -->
            <result name="error" type="redirect" >/frame/common/downloadError.jsp</result>
        </action>
     </package>

3.action

package com.pinzhi.action.common;


import java.io.InputStream;
import java.io.UnsupportedEncodingException;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.pinzhi.common.upload.UploadUtils;
import com.pinzhi.common.utils.Struts2Utils;
/**
 * 文件下载action
 * @author cqq
 *
 */
public class DownloadAction extends ActionSupport {
    private static final long serialVersionUID = 1L;
    
    Logger logger = Logger.getLogger(DownloadAction.class);
    
    //执行方法的顺序 set方法  execute getDownloadFile getFileName
    
    //下载的文件名
    private String name;
    //下载的文件路径
    private String filePath;
    
    /**
     * getFileName 此方法对应的是struts.xml文件中的:
     * <param name="contentDisposition">attachment;filename="${fileName}"
     * 这个属性设置的是下载工具下载文件时显示的文件名
     * @return
     * @throws UnsupportedEncodingException
     */
    public String getFileName() throws UnsupportedEncodingException{
        try {
            Struts2Utils.getResponse().setHeader("charset","ISO8859-1");
            name = new String(this.name.getBytes(), "ISO8859-1");
        } catch (UnsupportedEncodingException e) {
            logger.info("文件下载——获取文件名出错!");
            e.printStackTrace();
        }  
        return name;
    }
    /**
     * 此方法对应的是struts.xml文件中的:
     * <param  name="inputName">downloadFile
     * 返回下载文件的流
     * @return
     */
    public InputStream getDownloadFile(){
        InputStream istream = ServletActionContext.getServletContext().getResourceAsStream("/"+filePath);
        
        if(istream==null){
            logger.info("文件下载出错!路径不对,或文件不存在!");
        }
        return istream;
    }
    /**
     * <result name="success" type="stream">
     * 成功的返回文件流
     */
    @Override
    public String execute() throws Exception{
        InputStream istream = ServletActionContext.getServletContext().getResourceAsStream("/"+filePath);
        //如果文件不存在就跳转页面
        if(istream==null){
            return "error";
        }
        return SUCCESS;
    }
    
    //getter and setter
    public String getName() {
        return name;
    }
    public void setName(String name) {
        
        if(Struts2Utils.isNullOrEmptyString(name)) name = "file";
        String ext = UploadUtils.getExt(filePath);
        name = name + "." + ext;
        
        this.name = name;
    }
    public String getFilePath() {
        return filePath;
    }
    public void setFilePath(String filePath) {
        this.filePath = filePath;
    }




4.jsp调用

${ctx}/download.do?name=${obj.attachmentname}&filePath=${obj.attachmenturl}

    
    
}
原创粉丝点击