从页面中下载数据到Txt文件中

真个过程是主要用到Struts1的知识

 

 

 

 

 

action的代码:

 

response.setContentType("text/xml");
   response.setHeader("Cache-Control", "no-cache");
   response.setCharacterEncoding("UTF-8");
   StringBuffer downloadSend = new StringBuffer();
   try {
    List list = new ArrayList();
    actionForm.setPageSize(1000);
    bl.selectPage(request.getSession(), actionForm, 1);
    int total = actionForm.getTotalPage();
    for (int i = 1; i <= total; i++) {
     bl.selectPage(request.getSession(), actionForm, i);
     list.addAll(actionForm.getGridList());
    }
    Iterator it = list.iterator();
    downloadSend.append("编号,特服号/r/n");
    while (it.hasNext()) {
     TbSysServerCode data = (TbSysServerCode) it.next ();
     downloadSend.append(data.getId() + ",");
     downloadSend.append(data.getNo() + "/r/n");
    }
    request.setAttribute("fileName", CTime.getTime(CTime.YYMMDDhhmmssxxx));
    request.setAttribute("download", downloadSend);
   } catch (Exception e) {
    e.printStackTrace();
   }
   return mapping.findForward("TXT");

 

 

 

Dao的代码:

 

 public String selectPage(HttpSession session, ActionForm form, int start) {
  dao = new SysServerCodeDAO();
  SysServerCodeForm actionForm = (SysServerCodeForm) form;
  dao.setGridList(actionForm, start);
  return "";
 }

 

 

 

实现接口的类代码:

 

 public List setGridList(SysServerCodeForm actionForm, int start) {
  String condition = "";
  if (null != actionForm.getQueryNo() && !actionForm.getQueryNo().equals("")) {
   condition += " and no like '%" + actionForm.getQueryNo() + "%'";
  }
  String hql = "from TbSysServerCode where nstat = " + Constants.NSTATE_Normal + condition + " order by id";
  PageUtil pu = new PageUtil(start, hql, actionForm);
  List<TbSysServerCode> list = pu.getPage().getList();
  actionForm.setGridList(list);
  return list;
 }s

 

 

jsp页面代码:

<%@ page contentType='text/html; charset=utf-8'%>
<%@page import="java.io.PrintWriter"%>
<%
 
 response.setContentType("application/octet-stream;charset=gbk;");
 String fileName = (String)request.getAttribute("fileName");
    fileName = fileName == null ? System.currentTimeMillis()+"" : (fileName.length() < 0 ? System.currentTimeMillis()+"" : fileName);
 fileName = new String((fileName+".txt").getBytes("utf-8"),"iso8859-1");
 response.setHeader("Content-Disposition", "attachment;filename="+fileName);
 PrintWriter pw = response.getWriter();
 StringBuffer content = (StringBuffer)request.getAttribute("download");
 pw.print(content.toString());
 pw.flush();
 pw.close();
%>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值