文件下载struts2

第一步:struts2文件下载 xml配置

 

<action name="download"
            class="action.netDiskAction.DownloadAction">
            <!-- 文件名 -->
            <param name="fileName"></param>
            <result name="success" type="stream">
                <!-- 类型-->
                <param name="contentType">text/plain</param>
                <!-- 前台链接参数 -->
                <param name="contentDisposition">
                    attachment;filename="${downloadChineseFileName}"
                </param>
                <!--  -->
                <param name="inputName">downloadFile</param>
            </result>
            <result name="downloaderror" type="chain">
                showResList
            </result>
        </action>

 

第二步: 类的实现

package action.netDiskAction;

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

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

/**
 * 个人中心资源问答下载附件实现
 *
 * @author huyt
 *
 */
public class DownloadAction extends ActionSupport {

    private String fileName; // 文件名和文件路径

    private String newFileName; // 用于下载后显示的文件名

    private boolean isExists; // 用户判断文件是否存在

    private int toPage; // 下载资源所在的当前页面

    // 从下载文件原始存放路径读取得到文件输出流
    public InputStream getDownloadFile() {

        return ServletActionContext.getServletContext().getResourceAsStream(
                "/" + fileName);
    }

    // 如果下载文件名为中文,进行字符编码转换
    public String getDownloadChineseFileName() {

        String downloadChineseFileName = newFileName;

        try {
            downloadChineseFileName = new String(downloadChineseFileName
                    .getBytes(), "ISO8859-1");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        return downloadChineseFileName;
    }

    public String execute() {

        String basePath = ServletActionContext.getServletContext().getRealPath(
                "");
        String filePath = basePath + fileName;
        File file = new File(filePath);
        if (!file.exists()) {

            HttpServletRequest request = ServletActionContext.getRequest();
            request.setAttribute("message", "文件已经不存在,请联系管理员!");

            // 如果topage为0时,说明在第一页,需要进行重新设置为1
            if (toPage == 0) {
                toPage = 1;
            }

            return "downloaderror";

        } else {
            return SUCCESS;
        }
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public String getNewFileName() {
        return newFileName;
    }

    public void setNewFileName(String newFileName) {
        this.newFileName = newFileName;
    }

    public int getToPage() {
        return toPage;
    }

    public void setToPage(int toPage) {
        this.toPage = toPage;
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值