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>
Struts2 实现导出和下载的整合
最新推荐文章于 2019-06-06 15:34:00 发布