真个过程是主要用到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();
%>