Struts2 实现导出和下载的整合

package com.onepiece.action.manager;

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

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.onepiece.base.action.BaseAction;
import com.onepiece.domain.manager.Informations;
import com.onepiece.query.PageResult;
import com.onepiece.query.manager.InfoQuery;
import com.onepiece.service.manager.InfoService;
import com.onepiece.utils.Ouputone;
//本方法只是本项目要求,读者只需要获取主要内容即可

@SuppressWarnings("serial")
@Controller("downloadAction")
@Scope("prototype")
public class DownloadAction extends BaseAction<Informations> {
    private String searchText;
    private java.io.File excl; //上传的文件
    private String exclFileName="info.xls"; //文件名称,这里写死,其实是对应jsp的name的名字,命名保证为XXXFileName的格式
    private String exclContentType; //文件类型
    private InfoQuery baseQuery = new InfoQuery();
    @Resource(name = "infoService")
    private InfoService infoService;


    public String getSearchText() {
        return searchText;
    }
    public void setSearchText(String searchText) {
        String s = searchText;
        try {
            s = java.net.URLDecoder.decode(s, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        this.searchText = s;
    }
    public java.io.File getExcl() {
        return excl;
    }
    public void setExcl(java.io.File excl) {
        this.excl = excl;
    }
    public String getExclFileName() {
        return exclFileName;
    }
    public void setExclFileName(String exclFileName) {
        this.exclFileName = exclFileName;
    }
    public String getExclContentType() {
        return exclContentType;
    }
    public void setExclContentType(String exclContentType) {
        this.exclContentType = exclContentType;
    }
    public InfoQuery getBaseQuery() {
        return baseQuery;
    }
    public void setBaseQuery(InfoQuery baseQuery) {
        this.baseQuery = baseQuery;
    }
    //主要方法
    public InputStream getDownloadFile() throws Exception{
        InputStream in;
        if(this.searchText!=""){
            baseQuery.setSearchText(this.searchText);
        }
        if(this.getCurrentPage() != 0) {
            baseQuery.setCurrentPage(this.getCurrentPage());
        }
        if(this.getPageSize() != 0) {
            baseQuery.setPageSize(this.getPageSize());
        }
        PageResult<Informations> posiHoldList=this.infoService.getOtherPageInfo(baseQuery);
        //ServletActionContext.getContext().put("managerInfos", posiHoldList);
        String realPath = ServletActionContext.getServletContext().getRealPath(
                "/uploadhead/info.xls");
                //调用导出方法,前面文章提到过
        Ouputone.extport(posiHoldList.getRows(),realPath);
        //获取数据流
        in= ServletActionContext.getServletContext().getResourceAsStream( "/uploadhead/info.xls");
        if(in==null){
            System.out.println("路径错误!");
        }
        return in;
    }
    @Override
    public String execute() throws Exception {
        return SUCCESS;
    }
}
//struts.xml的配置
<struts>

     <package name="downloadAction" namespace="/" extends="struts-default">
        <action name="loadAction" class="downloadAction">
            <result name="success" type="stream">  
               <param name="contentType">application/vnd.ms-excel;charset=ISO8859-1</param>  //对应文件类型
               <param name="contentDisposition">attachment;filename="${exclFileName}"</param> //对应前面的属性的名字 
               <param name="inputName">downloadFile</param>  //对应action方法的getDownloadFile
               <param name="bufferSize">4096</param>  
             </result>  
        </action>
    </package>
</struts>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值