File file = null;
OutputStreamWriter out = null;
CsvWriter writer = null;
Page<Audit> page = getPage(pageNo, pageSize, begin, end, userId, opObject, opType);
List<Audit> list = page.getResult();
String fileName = DateUtil.format(new Date(), "yyyyMMddhhmmss");
fileName += (int)Math.random()*100 + ".csv";
FileUtils.makeDir(filePath);
try
{
file = new File(filePath + fileName);
out = new OutputStreamWriter(new FileOutputStream(file), ServletUtils.DEFAULT_ENCODING);
//set bom to utf-8 for excel to read
out.write(0xFEFF);
writer = new CsvWriter(out, ',');
writer.writeRecord(new String[]{"时间", "操作者", "IP", "事件类型", "操作对象", "结果", "备注"});
for(Audit audit:list){
writer.write(DateUtil.format(audit.getTime()));
writer.write(audit.getUserId());
writer.write(audit.getIp());
writer.write(audit.getOpType());
writer.write(audit.getOpObject());
writer.write(audit.getOpResult());
writer.write(audit.getOpMemo());
writer.endRecord();
}
writer.flush();
}
catch (IOException e)
{
throw new ServiceException("读写审计日志文件时出错!", e);
}finally{
if(writer!=null){
writer.close();
}
if(out!=null){
out.close();
}
}
return file;