导出文件,采用多态,多线程的设计方法。
定义一个 WriterUtil 的基类,写xls,txt,ndt等都继承其方法,同时重要的writeFile() 为了要复写,在基类抛出异常。
/**
* 是否剔除中间汇总节点
* @return
* @throws Exception
*/
public File writeFile() throws Exception {
throw new Exception("请实现该方法!");
}
在control中的下载方法中,采用多线程下载各个东西。
@ResponseBody
@RequestMapping("/ei/rwdata/download/start")
public void startDownload(HttpServletResponse response, @RequestParam int choosetype, @RequestParam String sid, HttpServletRequest request)
throws Exception {
long start = System.currentTimeMillis();
Login login = JspFunc.getLogin(request);
response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
request.setAttribute("choosetype", choosetype);
WriterParams params = getParamsMap(sid);
params.setType(choosetype);
WriterUtil util = null;
int type = params.getType();
StringBuffer log = new StringBuffer();//记录日志的信息
switch (type) {
case TYPE_PAT:
log.append("导出格式:").append("PAT").append("\r\n");
util = new PatWriterUtil(login, params);
break;
case TYPE_TXT:
log.append("导出格式:").append("TXT").append("\r\n");
util = new TxtWriterUtil(login, params);
break;
case TYPE_NDT:
log.append("导出格式:").append("NDT").append("\r\n");
util = new NdtWriterUtil(login, params);
break;
case TYPE_EXCEL:
log.append("导出格式:").append("EXCEL").append("\r\n");
util = new XlsWriterUtil(login, params);
break;
case TYPE_OEM_XML:
log.append("导出格式:").append("XML").append("\r\n");
util = new XmlWriterUtil(login, params);
break;
case TYPE_DAT:
log.append("导出格式:").append("DAT").append("\r\n");
util = new DatWriterUtil(login, params);
break;
case TYPE_EXCEL_ADBC_KHFX:
log.append("导出格式:").append("DAT").append("\r\n");
util = new XlsWriterUtilForKHFX(login, params);
break;
}
log.append("任务:").append(params.getTask().getCaption()).append("\r\n");
log.append("数据集:").append(params.getDataset()).append("\r\n");
if (params.getBbqs() != null || !StrFunc.isNull(params.getCbbq())) {
log.append("报表期:");
String[] bbqs = params.getBbqs();
if (bbqs != null && bbqs.length > 0) {
log.append(ArrayFunc.array2Str(bbqs, ","));
} else {
log.append(params.getCbbq());
}
}
Bbh cbbh = params.getCbbh();
if (cbbh != null) {
log.append("\r\n当前报表户:").append(cbbh.getName()).append("(").append(cbbh.getId()).append(")").append("\r\n");
}
int op = params.getDwOp();
String[] dwop = new String[] { "本级单位(作为基层单位上报)", "本级及直接下级单位(下级单位作为基层单位上报)", "所有单位", "已选择的单位" };
if (op > -1 && op < dwop.length) {
log.append("导出单位选项:").append(dwop[op]).append("\r\n");
if (op == WriterUtil.UPSELECTED) {
//解决添加多标签选中报表户串的问题
String id = StrFunc.null2blank(params.getTabid())+params.getTask().getServerId();
BbhBookMarks marks = BbhBookMarkUtil.getBbhBookMarks(login, id);
if (marks != null) {
int count = marks.getCount();
log.append("选择户数:").append(count).append("\r\n");
if (count <= 10) {
String[] bbhs = marks.getBbhs();
log.append("选择的户:").append(ArrayFunc.array2Str(bbhs, ";")).append("\r\n");
}
}
}
}
WriteFileThread thread = new WriteFileThread(util);
/**
* IRPT-21919 edit by ligl on 20160223 导出的excel和导出数据明显感觉时间不对,都是0
*/
thread.setStartTime(start);
thread.setLogs(log.toString());
threadMap.put(sid, thread);
Thread realthread = new Thread(thread);
realthread.start();
// ReportLogManager.addInfoLog(login, LogedOperations.OP_BBHDATA_EXPORTDATA, start, null, params.getTask().getGroup(),
// params.getTask().getServerId(), params.getCbbq(), cbbh.getId(), cbbh.getType(), "导出报表户数据", log.toString(), null);
}
WriteFileThread 类实现了Runnable接口。
其run方法,调用具体的文件的写util类。
public void run() {
try {
file = util.writeFile();
}
catch (CancelException e) {
util.addLog("导出操作被取消!");
}
catch (Exception e) {
ReportLogManager.addWarnLog(null, null, null, null, null, null, null, null,
e.getMessage(), StrFunc.exception2str(e), null);
ex = e;
util.addLog(StrFunc.exceptionMsg2str(e));
}
finally {
finished = true;
}
}