81,094
社区成员
发帖
与我相关
我的任务
分享
function setAuto(time) {
var intervalTime = time;
var interval = setInterval("auto()", intervalTime * 1000); // 定时执行action中的loding方法
var percent = document.forms[0].percent.value;
if (percent != null && percent == '100') {
clearInterval(interval); // 如果action中的loding方法得到的全局变量percent为100,证明线程已经结束(线程执行的步骤只是查询数据库和组织beanList,然后放session里),停掉定时
csvDownload(); // 执行action中的csvDownload方法,从session中获取到beanList,然后完成download
}
}
public ActionForward doloading(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest request,
HttpServletResponse response) throws Exception {
if (loadingBean.getPercent() == 100) {
logger.info("AllCustAccountInfoAction.doloading() ==> success!!! ");
return actionMapping.findForward("success");
}
request.setAttribute("percent", loadingBean.getPercent());
return actionMapping.findForward("loading");
}
public static void exportCsv(HttpServletResponse response, String file, List<String[]> rows) throws IOException {
StringBuffer record = new StringBuffer();
for (int i = 0; i < rows.size(); i++) {
String[] cells = rows.get(i);
if (cells != null) {
for (int j = 0; j < cells.length; j++) {
record.append(cells[j] + ",");
}
record.append("\r\n");
}
}
response.reset();
response.setContentType("Application/text/csv");
response.setHeader("Content-Disposition", "attachment;filename=\"" + URLEncoder.encode(file, DEFAULT_ENCODING)
+ "\"");
ServletOutputStream servletout = response.getOutputStream();
PrintWriter outpw = new PrintWriter(new OutputStreamWriter(servletout, SHIFTJIS_ENCODING));
outpw.write(record.toString());
outpw.close();
servletout.close();
}
public void run() {
AllCustAccountInfoService aais = new AllCustAccountInfoService();
try {
CsvExportUtil.exportCsv(loadingBean.getResponse(), loadingBean.getFileName(), aais
.buildCsvFile(loadingBean));
} catch (Exception e) {
logger.error("AllCustAccountInfoAction.run() exportCsv error!!!", e);
}
loadingBean.setPercent(100);// 线程执行结束设置百分比为100
}
public ActionForward doexportCsv(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest request,
HttpServletResponse response) throws Exception {
Thread exportCsvThread = new Thread(this, "");
exportCsvThread.setName("loading");
loadingBean = new LoadingBean();
loadingBean.setOpenStatus(request.getParameter("openStatus"));
loadingBean.setFileName("customerinfo.csv");
loadingBean.setResponse(response);
loadingBean.setPercent(0); // 起始的百分比,线程执行的过程中根据执行情况而不断变化
exportCsvThread.start();
return actionMapping.findForward("loading");
}