文件上传
This commit is contained in:
parent
3e80f8843c
commit
3d4314e0dd
@ -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<BaseResponse<Object>> 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -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;
|
||||||
|
}
|
157
src/main/java/com/wxjw/service/GetUploadFileService.java
Normal file
157
src/main/java/com/wxjw/service/GetUploadFileService.java
Normal file
@ -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<BaseResponse<Object>> 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user