From 3d4314e0dda7d494f7cd15e7f8d35923001223c3 Mon Sep 17 00:00:00 2001 From: XiaoLFeng Date: Thu, 17 Aug 2023 10:42:04 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=87=E4=BB=B6=E4=B8=8A=E4=BC=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../openapi/GetUploadFileController.java | 38 +++++ .../data/GetUploadFile/GetUploadFileData.java | 20 +++ .../wxjw/service/GetUploadFileService.java | 157 ++++++++++++++++++ 3 files changed, 215 insertions(+) create mode 100644 src/main/java/com/wxjw/controller/openapi/GetUploadFileController.java create mode 100644 src/main/java/com/wxjw/dal/pojo/data/GetUploadFile/GetUploadFileData.java create mode 100644 src/main/java/com/wxjw/service/GetUploadFileService.java diff --git a/src/main/java/com/wxjw/controller/openapi/GetUploadFileController.java b/src/main/java/com/wxjw/controller/openapi/GetUploadFileController.java new file mode 100644 index 0000000..d8ffbad --- /dev/null +++ b/src/main/java/com/wxjw/controller/openapi/GetUploadFileController.java @@ -0,0 +1,38 @@ +package com.wxjw.controller.openapi; + +import com.wxjw.common.BaseResponse; +import com.wxjw.common.ResultUtil; +import com.wxjw.dal.dao.ExcelInfoMapper; +import com.wxjw.dal.pojo.ErrorCode; +import com.wxjw.dal.pojo.data.GetUploadFile.GetUploadFileData; +import com.wxjw.service.GetUploadFileService; +import jakarta.annotation.Resource; +import org.jetbrains.annotations.NotNull; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + * 文件上传 + * + * @author 筱锋xiao_lfeng + */ +@RestController +@RequestMapping("/openapi/files") +public class GetUploadFileController { + @Resource + private ExcelInfoMapper excelInfoMapper; + + @PostMapping("/upload") + public ResponseEntity> getUploadFile(@RequestBody @NotNull GetUploadFileData getUploadFileData) { + if ("importfile".equals(getUploadFileData.getAction())) { + GetUploadFileService getUploadFileService = new GetUploadFileService(excelInfoMapper); + getUploadFileService.uploadFileService(getUploadFileData); + return getUploadFileService.getReturnResult(); + } else { + return ResultUtil.error(ErrorCode.PARAMETER_ERROR); + } + } +} diff --git a/src/main/java/com/wxjw/dal/pojo/data/GetUploadFile/GetUploadFileData.java b/src/main/java/com/wxjw/dal/pojo/data/GetUploadFile/GetUploadFileData.java new file mode 100644 index 0000000..6f8411e --- /dev/null +++ b/src/main/java/com/wxjw/dal/pojo/data/GetUploadFile/GetUploadFileData.java @@ -0,0 +1,20 @@ +package com.wxjw.dal.pojo.data.GetUploadFile; + +import jakarta.annotation.Nullable; +import lombok.Data; +import lombok.experimental.Accessors; + +/** + * + * + * @author 筱锋xiao_lfeng + */ +@Data +@Accessors(chain = true) +public class GetUploadFileData { + private String action; + private String userid; + @Nullable + private String filelib; + private Object files; +} diff --git a/src/main/java/com/wxjw/service/GetUploadFileService.java b/src/main/java/com/wxjw/service/GetUploadFileService.java new file mode 100644 index 0000000..364a937 --- /dev/null +++ b/src/main/java/com/wxjw/service/GetUploadFileService.java @@ -0,0 +1,157 @@ +package com.wxjw.service; + +import com.wxjw.common.BaseResponse; +import com.wxjw.common.ResultUtil; +import com.wxjw.dal.dao.ExcelInfoMapper; +import com.wxjw.dal.pojo.ErrorCode; +import com.wxjw.dal.pojo.data.GetUploadFile.GetUploadFileData; +import com.wxjw.dal.pojo.entity.ExcelInfoEntity; +import lombok.Getter; +import org.apache.poi.EncryptedDocumentException; +import org.apache.poi.ss.usermodel.*; +import org.jetbrains.annotations.NotNull; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Service; + +import java.io.*; +import java.text.SimpleDateFormat; +import java.util.Base64; +import java.util.Date; + +/** + * @author 筱锋xiao_lfeng + */ +@Service +public class GetUploadFileService { + + private final ExcelInfoMapper excelInfoMapper; + @Getter + private ResponseEntity> returnResult; + @Getter + private boolean checkType; + + public GetUploadFileService(ExcelInfoMapper excelInfoMapper) { + this.excelInfoMapper = excelInfoMapper; + } + + private static boolean isSheetEmpty(@NotNull Sheet sheet) { + for (int i = sheet.getFirstRowNum(); i <= sheet.getLastRowNum(); i++) { + Row row = sheet.getRow(i); + if (row != null) { + for (int j = row.getFirstCellNum(); j < row.getLastCellNum(); j++) { + Cell cell = row.getCell(j); + if (cell != null && cell.getCellType() != CellType.BLANK) { + return false; + } + } + } + } + return true; + } + + private static void copySheet(@NotNull Sheet sourceSheet, Sheet targetSheet) { + for (int i = sourceSheet.getFirstRowNum(); i <= sourceSheet.getLastRowNum(); i++) { + Row sourceRow = sourceSheet.getRow(i); + if (sourceRow != null) { + Row targetRow = targetSheet.createRow(i); + + for (int j = sourceRow.getFirstCellNum(); j < sourceRow.getLastCellNum(); j++) { + Cell sourceCell = sourceRow.getCell(j); + if (sourceCell != null) { + Cell targetCell = targetRow.createCell(j, sourceCell.getCellType()); + Object cellValue = getCellValue(sourceCell); + targetCell.setCellValue(cellValue.toString()); + } + } + } + } + } + + private static Object getCellValue(@NotNull Cell cell) { + return switch (cell.getCellType()) { + case NUMERIC -> cell.getNumericCellValue(); + case STRING -> cell.getStringCellValue(); + case BOOLEAN -> cell.getBooleanCellValue(); + case BLANK -> ""; + // Handle other cell types if needed + default -> null; + }; + } + + public void uploadFileService(@NotNull GetUploadFileData getUploadFileData) { + // 获取文件 + if (getUploadFileData.getFiles() != null) { + // 获取 base64 文件流 + byte[] base64 = Base64.getDecoder().decode(String.valueOf(getUploadFileData.getFiles())); + // 检查文件尾缀是否正确 + InputStream inputStream = new ByteArrayInputStream(base64); + try { + Workbook workBook = WorkbookFactory.create(inputStream); + String getFileType = workBook.getSpreadsheetVersion().toString().toLowerCase(); + if (getFileType.contains("excel2007") || getFileType.contains("excel97") || getFileType.contains("wps")) { + // 文件类型正确(存储) + long time = new Date().getTime(); + // 设置文件存储位置 + String path = "./src/main/resources/excel/" + time + "/"; + String fileName = null; + if (getFileType.contains("excel2007")) { + fileName = time + ".xlsx"; + } else if (getFileType.contains("excel97")) { + fileName = time + ".xls"; + } + // 创建文件 + File file = new File(path); + if (!file.exists()) { + if (file.mkdirs()) { + // 成功 + for (int i = 0; i < workBook.getNumberOfSheets(); i++) { + Sheet sheet = workBook.getSheetAt(i); + // 检查 sheet 是否存在 + if (!isSheetEmpty(sheet)) { + if (getFileType.contains("excel2007")) { + fileName = sheet.getSheetName() + ".xlsx"; + } else if (getFileType.contains("excel97")) { + fileName = sheet.getSheetName() + ".xls"; + } + try { + Workbook targetWorkbook = WorkbookFactory.create(true); + + Sheet targetSheet = targetWorkbook.createSheet(sheet.getSheetName()); + copySheet(workBook.getSheet(sheet.getSheetName()), targetSheet); + + try (FileOutputStream fileOutputStream = new FileOutputStream(file + "/" + fileName)) { + targetWorkbook.write(fileOutputStream); + // 载入数据库 + ExcelInfoEntity excelInfoEntity = new ExcelInfoEntity(); + excelInfoEntity + .setFileName(String.valueOf(time)) + .setSheetName(sheet.getSheetName()) + .setType(0) + .setCreateBy(getUploadFileData.getUserid()) + .setCreateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())); + excelInfoMapper.insertExcelInfo(excelInfoEntity); + returnResult = new GetFileTreeService().getFileTreeService(excelInfoMapper); + } + } catch (IOException | EncryptedDocumentException e) { + throw new RuntimeException(e); + } + } + } + } else { + // 文件夹创建失败 + returnResult = ResultUtil.error(ErrorCode.FILE_CREATION_FAILED); + } + } else { + // 文件存在 + returnResult = ResultUtil.error(ErrorCode.FILE_ALREADY_EXISTS); + } + } else { + // 文件类型不正确 + returnResult = ResultUtil.error(ErrorCode.FILE_TYPE_IS_INCORRECT); + } + } catch (IOException e) { + throw new RuntimeException(e); + } + } + } +}