diff --git a/src/main/java/com/system/log/controller/LogController.java b/src/main/java/com/system/log/controller/LogController.java index 6f36091..6de3d5d 100644 --- a/src/main/java/com/system/log/controller/LogController.java +++ b/src/main/java/com/system/log/controller/LogController.java @@ -66,7 +66,7 @@ public class LogController extends JpaSupport { // return logService.getFileChange(); // } - public void exportLog(ActionRequest request, ActionResponse response) { + public void exportLog(ActionRequest request, ActionResponse response) throws IOException { File file = logService.exportLog(request, response); FileUtils.downloadFile(response, file); } diff --git a/src/main/java/com/system/log/service/LogService.java b/src/main/java/com/system/log/service/LogService.java index d2a9046..d563e33 100644 --- a/src/main/java/com/system/log/service/LogService.java +++ b/src/main/java/com/system/log/service/LogService.java @@ -20,6 +20,10 @@ import net.lingala.zip4j.exception.ZipException; import net.lingala.zip4j.model.ZipParameters; import net.lingala.zip4j.util.Zip4jConstants; import org.apache.commons.lang3.StringUtils; +import org.apache.poi.hssf.usermodel.HSSFCell; +import org.apache.poi.hssf.usermodel.HSSFRow; +import org.apache.poi.hssf.usermodel.HSSFSheet; +import org.apache.poi.hssf.usermodel.HSSFWorkbook; public class LogService { @@ -327,12 +331,13 @@ public class LogService { // return getFileContent(fileList, null, null, null, null); // } - public File exportLog(ActionRequest request, ActionResponse response) { + public File exportLog(ActionRequest request, ActionResponse response) throws IOException { String password = request.getContext().get("password").toString(); String confirmPassword = request.getContext().get("confirmPassword").toString(); String startTime = request.getContext().get("exportStartTime").toString(); String endTime = request.getContext().get("exportEndTime").toString(); if (!password.equals(confirmPassword)) { + response.setAlert("两次输入的密码不一致!"); return null; } // 密码正则表达式校验 @@ -343,6 +348,7 @@ public class LogService { if (!matcher.find()) { // 密码不通过正则校验 response.setAlert("密码需要包含6-16位数字+大写字母+小写字母+特殊字符!"); + return null; } List selectResult = new ArrayList<>(); @@ -381,8 +387,8 @@ public class LogService { selectResult.add(logEntity); } } - Object jsonObject = JSONObject.toJSON(selectResult); - return exportLogZip(jsonObject, password); + //Object jsonObject = JSONObject.toJSON(selectResult); + return exportLogExcelZip(selectResult, password); } private long getSqlLongTime(Object time) { @@ -403,6 +409,92 @@ public class LogService { return date.getTime(); } + //导出为Excel类型 + public File exportLogExcelZip(List selectResult, String password) throws IOException { + //创建一个工作簿,也就是Excel文件 + HSSFWorkbook wb = new HSSFWorkbook(); + //创建一个工作表 + HSSFSheet sheet = wb.createSheet(); + //第一行,标题 + HSSFRow row0 = sheet.createRow(0); + HSSFCell cell0 = row0.createCell(0); + cell0.setCellValue("操作日志表"); + //第二行,表头 + HSSFRow row1 = sheet.createRow(1); + row1.createCell(0).setCellValue("id"); + row1.createCell(1).setCellValue("address"); + row1.createCell(2).setCellValue("content"); + row1.createCell(3).setCellValue("hashValue"); + row1.createCell(4).setCellValue("levelDesc"); + row1.createCell(5).setCellValue("levelNum"); + row1.createCell(6).setCellValue("module"); + row1.createCell(7).setCellValue("objectName"); + row1.createCell(8).setCellValue("opType"); + row1.createCell(9).setCellValue("result"); + row1.createCell(10).setCellValue("resultDesc"); + row1.createCell(11).setCellValue("status"); + row1.createCell(12).setCellValue("time"); + row1.createCell(13).setCellValue("userAuth"); + row1.createCell(14).setCellValue("userAuthDesc"); + row1.createCell(15).setCellValue("userId"); + + //向表中写入数据 + int rowInt = 2; + for (LogEntity logEntity : selectResult) { + HSSFRow row = sheet.createRow(rowInt++); + int index = 0; + row.createCell(index++).setCellValue(logEntity.getId()); + row.createCell(index++).setCellValue(logEntity.getAddress()); + row.createCell(index++).setCellValue(logEntity.getContent()); + row.createCell(index++).setCellValue(logEntity.getHashValue()); + row.createCell(index++).setCellValue(logEntity.getLevelDesc()); + row.createCell(index++).setCellValue(logEntity.getLevelNum().getValue()); + row.createCell(index++).setCellValue(logEntity.getModule()); + row.createCell(index++).setCellValue(logEntity.getObjectName()); + row.createCell(index++).setCellValue(logEntity.getOpType().getValue()); + row.createCell(index++).setCellValue(logEntity.getResult()); + row.createCell(index++).setCellValue(logEntity.getResultDesc()); + row.createCell(index++).setCellValue(logEntity.getStatus()); + row.createCell(index++).setCellValue(logEntity.getTime().toString()); + row.createCell(index++).setCellValue(logEntity.getUserAuth().getValue()); + row.createCell(index++).setCellValue(logEntity.getUserAuthDesc()); + row.createCell(index++).setCellValue(logEntity.getUserId()); + } + FileOutputStream fos = null; + try { + //输出文件,创建字节输出流 + File tempFile = new File(File.createTempFile("log", ".xlsx").getPath()); + fos = new FileOutputStream(tempFile); + wb.write(fos); + fos.flush(); + + // 根据时间设定输出文件名 + SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss"); + String date = simpleDateFormat1.format(new Date(System.currentTimeMillis())); + + // 获取临时文件目录 + String tempDir = System.getProperty("java.io.tmpdir"); + String tempFilePath = tempDir + "/" + date + ".zip"; + System.out.println(tempFilePath); + // 在临时文件目录中创建一个随机名称的zip文件 + File file = new File(tempFilePath); + ZipFile zipFile = new ZipFile(file); + ZipParameters parameters = new ZipParameters(); // 设置zip包的一些参数集合 + parameters.setEncryptFiles(true); // 是否设置密码(此处设置为:是) + parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE); // 压缩方式(默认值) + parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL); // 普通级别(参数很多) + parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_STANDARD); // 加密级别 + parameters.setPassword(password); // 压缩包密码 + // 向zip文件中根据设置的参数写入数据 + zipFile.createZipFile(tempFile, parameters); + return file; + } catch (ZipException e) { + throw new RuntimeException(e); + } finally { + fos.close(); + } + } + public File exportLogZip(Object selectResult, String password) { try { // 创建临时文件,将查询的结果写入临时文件