当前位置:首页 > 开发 > 编程语言 > Struts2 > 正文

Struts2文件下载

发表于: 2013-10-16   作者:ak23173969   来源:转载   浏览:
摘要: struts2实现文件下载需求三个步骤 1 配置对应的jsp,通过链接传递参数 2 第二配置struts配置文件 3 配置相应的action   JXLUtil.getSentMsgTotaldBox_Result() 返回的是excel文件的临时存储目录,将数据中取出来的信息写成excel文件,然后保存在我指定的路径下,再将文件存储的相对路径告诉struts,放到输入流里去,

struts2实现文件下载需求三个步骤

1 配置对应的jsp,通过链接传递参数

2 第二配置struts配置文件

3 配置相应的action

 

JXLUtil.getSentMsgTotaldBox_Result() 返回的是excel文件的临时存储目录,将数据中取出来的信息写成excel文件,然后保存在我指定的路径下,再将文件存储的相对路径告诉struts,放到输入流里去,由浏览器(客户端)去执行下载,读出字节流中的文件

 

jsp  因为是通过链接的方式,所以属于get提交,参数必须绑定在链接上

<input type="button"  value="下载结果"  onclick="down_TotalMsg_result()" />

 

/**
* 下载短信记录excel文件
*/
function down_TotalMsg_result(){
	
	if(!check_time()){
		return ;
	}else{
		var time1=$("#time1").val();
		var time2=$("#time2").val();
		var time3=$("#time3").val();
		var time4=$("#time4").val();
		var time5=$("#time5").val();
		var time6=$("#time6").val();
		var content=$("#content").val();
		var tel=$("#tel").val();
		var agency=$("#agency").val();
		var channelname=$("#channelname").val();
		var nstate=$("#nstate").val();
		
		window.location.href="/smsCore/sendMsgSerach_down.action?fileName=sendTotalResult&&sentTime1="
		+time3+"&&sentTime2="+time4+"&&subTime1="+time1+"&&subTime2="+time2+"&&reportTime1="+time5+"&&reportTime2="+time6+
		"&&content="+content+"&&tel="+tel+"&&agency="+agency+"&&channelname="+channelname+"&&nsate="+nstate;
	}
	
}

 

 

struts.xml

 

		<action name="sendMsgSerach_*" class="com.kjkj.ema.view.action.SendMsgSerachAction" method="{1}">
			<result>/WEB-INF/page/statistics/sendMsgSerach.jsp</result>
				<result name="tofactorsuccess">/WEB-INF/page/factor/tofactorySentMsgDetails.jsp</result>
				<result name="excel" type="stream">
					 <!--<param name="directory">D:/download/</param>-->   
				可以通配置文件注入下载文件的目标目录,只需要在action中提供相应的

					<param name="contentType">application/vnd.ms-excel</param>
					<param name="contentDisposition">filename="${fileName}.xls"</param>
					<param name="inputName">inputStream</param>
					 <param name="bufferSize">4096</param>
				</result>
			</action>

 

 

 

action

 

package com.kjkj.ema.view.action;

import java.io.FileInputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;

import com.kjkj.ema.domain.Smsreport;
import com.kjkj.ema.domain.User;
import com.kjkj.ema.pager.Pager;
import com.kjkj.ema.pager.SmsreportQuery;
import com.kjkj.ema.service.SmsreportService;
import com.kjkj.ema.service.UserService;
import com.kjkj.ema.utils.CommonUtils;
import com.kjkj.ema.utils.JXLUtil;


@SuppressWarnings("serial")
public class SendMsgSerachAction extends BaseAction{
	private SmsreportQuery baseQuery = new SmsreportQuery();
	private Pager<Smsreport> pager = null;
	private Long id;
	private Integer tag;
	private SmsreportService smsreportService;
	private UserService userService;
	private String fileName;
	private String content;
	private String tel;
	private String agency;
	private String channelname;
	private String nsate;
	private String sentTime1;
	private String sentTime2;
	private String subTime1;
	private String subTime2;
	private String reportTime1;
	private String reportTime2;
	
	
	/**下载数据*/
	public String down(){
		return "excel";
	}

	public InputStream getInputStream() throws Exception {
		System.out.println("进来");
		if(StringUtils.isNotBlank(subTime1)&&StringUtils.isNotBlank(subTime2)){
			StringBuilder bs=new StringBuilder();
			bs.append(subTime1).append(" 00:01");
			SimpleDateFormat f=new SimpleDateFormat("yyyy-MM-dd");
			Date da=f.parse(bs.toString());
			baseQuery.setSubmittimeStart(da);
			bs.delete(0, bs.length());
			bs.append(subTime1).append(" 00:01");
		    da=f.parse(bs.toString());
			baseQuery.setSubmittimeEnd(da);
		}
		if(StringUtils.isNotBlank(sentTime1)&&StringUtils.isNotBlank(sentTime2)){
			StringBuilder bs=new StringBuilder();
			bs.append(sentTime1).append(" 00:01");
			SimpleDateFormat f=new SimpleDateFormat("yyyy-MM-dd");
			Date da=f.parse(bs.toString());
			baseQuery.setSendtimeStart(da);
			bs.delete(0, bs.length());
			bs.append(sentTime2).append(" 00:01");
			da=f.parse(bs.toString());
			baseQuery.setSendtimeEnd(da);
		}
		if(StringUtils.isNotBlank(reportTime1)&&StringUtils.isNotBlank(reportTime2)){
			StringBuilder bs=new StringBuilder();
			bs.append(reportTime1).append(" 00:01");
			SimpleDateFormat f=new SimpleDateFormat("yyyy-MM-dd");
			Date da=f.parse(bs.toString());
			baseQuery.setReporttimeStart(da);
			bs.delete(0, bs.length());
			bs.append(reportTime2).append(" 00:01");
			da=f.parse(bs.toString());
			baseQuery.setReporttimeEnd(da);
		}
		if(StringUtils.isNotBlank(content)){
			baseQuery.setContent(content);
		}
		if(StringUtils.isNotBlank(agency))
		{	
			baseQuery.setAgency(agency);
		}
		if(StringUtils.isNotBlank(channelname)){
			baseQuery.setChannel(channelname);
		}
		if(StringUtils.isNotBlank(tel))
		{	baseQuery.setTel(tel);
		}
		if(-1!=Integer.parseInt(nsate)){
			baseQuery.setNstate(Integer.parseInt(nsate));
		}
		baseQuery.setPageSize(10000);
		
		String filepath = ServletActionContext.getServletContext().getRealPath(
				"/dowonload");
		Pager<Smsreport> pager=smsreportService.findGager2(baseQuery);
		
		filepath=JXLUtil.getSentMsgTotaldBox_Result(filepath,pager.getPageList());
 		
		return new FileInputStream(filepath);
	}
	

	get  and set  在此处省略

}

 

 

 

 

Struts2文件下载

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号