系统集成
This commit is contained in:
parent
a4aab7d02e
commit
f929928a18
|
|
@ -0,0 +1,41 @@
|
|||
package com.bonus.web.controller.common;
|
||||
|
||||
import com.bonus.common.core.domain.AjaxResult;
|
||||
import com.bonus.web.service.common.CommonUploadService;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
/**
|
||||
* @className:UploadFileController
|
||||
* @author:cwchen
|
||||
* @date:2025-10-16-13:43
|
||||
* @version:1.0
|
||||
* @description:文件上传公共方法
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/commonUpload")
|
||||
@Slf4j
|
||||
public class CommonUploadController {
|
||||
|
||||
@Resource(name = "CommonUploadService")
|
||||
private CommonUploadService service;
|
||||
|
||||
@ApiOperation(value = "上传小文件---5M以内", notes = "上传文件无需OCR识别")
|
||||
@PostMapping(value = "uploadSmallFile")
|
||||
private AjaxResult uploadSmallFile(@RequestParam(name = "file",required = true) MultipartFile file,@RequestParam(name = "params",required = true)String params) throws Exception {
|
||||
return service.uploadSmallFile(file,params);
|
||||
}
|
||||
|
||||
@ApiOperation(value = "上传小文件---5M以内", notes = "上传文件并使用OCR识别")
|
||||
@PostMapping(value = "uploadSmallFileByOcr")
|
||||
private AjaxResult uploadSmallFileByOcr(@RequestParam(name = "file",required = false) MultipartFile file,String params){
|
||||
return service.uploadSmallFileByOcr(file,params);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,12 +0,0 @@
|
|||
package com.bonus.web.controller.common;
|
||||
|
||||
/**
|
||||
* @className:UploadFileController
|
||||
* @author:cwchen
|
||||
* @date:2025-10-16-13:43
|
||||
* @version:1.0
|
||||
* @description:文件上传
|
||||
*/
|
||||
|
||||
public class CommonUploadFileController {
|
||||
}
|
||||
|
|
@ -1,23 +1,24 @@
|
|||
package com.bonus.web.controller.file;
|
||||
|
||||
import com.bonus.common.core.domain.AjaxResult;
|
||||
import com.bonus.common.domain.file.po.ResourceFilePo;
|
||||
import com.bonus.common.domain.file.vo.SysFile;
|
||||
import com.bonus.common.domain.ocr.dto.OcrRequest;
|
||||
import com.bonus.common.domain.ocr.vo.OcrResponse;
|
||||
import com.bonus.file.config.MinioConfig;
|
||||
import com.bonus.file.service.FileUploadService;
|
||||
import com.bonus.file.service.SourceFileService;
|
||||
import com.bonus.file.util.FileUtil;
|
||||
import com.bonus.file.util.MinioUtil;
|
||||
import com.bonus.ocr.service.OcrService;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @className:FileUploadController
|
||||
|
|
@ -33,6 +34,9 @@ public class FileUploadController {
|
|||
@Resource(name = "FileUploadService")
|
||||
private FileUploadService fileUploadService;
|
||||
|
||||
@Resource(name = "SourceFileService")
|
||||
private SourceFileService sourceFileService;
|
||||
|
||||
@Resource(name = "OcrService")
|
||||
private OcrService ocrService;
|
||||
|
||||
|
|
@ -69,4 +73,12 @@ public class FileUploadController {
|
|||
System.err.println("文件对象:{}" + file);
|
||||
return AjaxResult.success();
|
||||
}
|
||||
|
||||
@PostMapping(value = "addFile")
|
||||
private AjaxResult addFile(@RequestBody ResourceFilePo filePo) {
|
||||
List<ResourceFilePo> list = new ArrayList<>();
|
||||
list.add(filePo);
|
||||
sourceFileService.saveResourceFile(list);
|
||||
return AjaxResult.success();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,108 @@
|
|||
package com.bonus.web.service.common;
|
||||
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.bonus.common.constant.Constants;
|
||||
import com.bonus.common.core.domain.AjaxResult;
|
||||
import com.bonus.common.domain.ocr.dto.OcrRequest;
|
||||
import com.bonus.common.domain.ocr.vo.OcrResponse;
|
||||
import com.bonus.file.config.MinioConfig;
|
||||
import com.bonus.file.enums.UploadSuffixEnum;
|
||||
import com.bonus.file.service.FileUploadService;
|
||||
import com.bonus.file.util.FileUtil;
|
||||
import com.bonus.file.util.MinioUtil;
|
||||
import com.bonus.ocr.service.OcrService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.io.File;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @className:CommonUploadService
|
||||
* @author:cwchen
|
||||
* @date:2025-10-17-16:37
|
||||
* @version:1.0
|
||||
* @description:文件上传公共方法实现层
|
||||
*/
|
||||
@Service(value = "CommonUploadService")
|
||||
@Slf4j
|
||||
public class CommonUploadService {
|
||||
|
||||
@Resource(name = "FileUploadService")
|
||||
private FileUploadService fileUploadService;
|
||||
|
||||
@Resource(name = "OcrService")
|
||||
private OcrService ocrService;
|
||||
|
||||
@Resource
|
||||
private MinioConfig minioConfig;
|
||||
|
||||
@Resource
|
||||
private MinioUtil minioUtil;
|
||||
|
||||
/**
|
||||
* 上传小文件---5M以内 --- 上传文件无需OCR识别
|
||||
* @param file
|
||||
* @param params
|
||||
* @return AjaxResult
|
||||
* @author cwchen
|
||||
* @date 2025/10/17 16:42
|
||||
*/
|
||||
public AjaxResult uploadSmallFile(MultipartFile file, String params) {
|
||||
// params(json字符串) 参数需要传 suffix等参数
|
||||
JSONObject jsonObj = JSONObject.parseObject(params);
|
||||
// 获取文件前缀
|
||||
String suffix = UploadSuffixEnum.getValueByKey(jsonObj.getString("suffix"));
|
||||
// 生成文件路径
|
||||
String uploadPath = FileUtil.generateDatePath(file, suffix).replace("\\", "/");
|
||||
// 上传结果
|
||||
String uploadResult = fileUploadService.uploadFile(file, uploadPath);
|
||||
if(StringUtils.isEmpty(uploadResult)){
|
||||
return AjaxResult.error(Constants.UPLOAD_ERROR);
|
||||
}
|
||||
return AjaxResult.success(Constants.UPLOAD_SUCCESS,uploadPath);
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传小文件---5M以内 --- 上传文件并使用OCR识别
|
||||
* @param file
|
||||
* @param params
|
||||
* @return AjaxResult
|
||||
* @author cwchen
|
||||
* @date 2025/10/17 16:42
|
||||
*/
|
||||
public AjaxResult uploadSmallFileByOcr(MultipartFile file, String params) {
|
||||
// params(json字符串) 参数需要传 suffix(文件保存前缀)、fields_json(ocr识别的参数["姓名","性别"])等参数
|
||||
JSONObject jsonObj = JSONObject.parseObject(params);
|
||||
// 获取文件前缀
|
||||
String suffix = UploadSuffixEnum.getValueByKey(jsonObj.getString("suffix"));
|
||||
// 生成文件路径
|
||||
String uploadPath = FileUtil.generateDatePath(file, suffix).replace("\\", "/");
|
||||
String uploadResult = fileUploadService.uploadFile(file, uploadPath);
|
||||
if(StringUtils.isEmpty(uploadResult)){
|
||||
return AjaxResult.error(Constants.UPLOAD_ERROR);
|
||||
}
|
||||
OcrResponse ocrResponse = null;
|
||||
try {
|
||||
// 获取Minio临时文件
|
||||
File fileFromMinio = minioUtil.getFileFromMinio(minioConfig.getBucketName(), uploadPath);
|
||||
// 创建OCR识别请求参数
|
||||
OcrRequest ocrRequest = new OcrRequest();
|
||||
ocrRequest.setFile(fileFromMinio);
|
||||
ocrRequest.setType(FileUtil.getFileMimeType(file));
|
||||
ocrRequest.setFields_json(jsonObj.getString("fields_json"));
|
||||
// 调用ocr识别服务
|
||||
ocrResponse = ocrService.callOcrService(ocrRequest);
|
||||
} catch (Exception e) {
|
||||
log.error(e.toString(),e.getMessage());
|
||||
}
|
||||
Map<String, Object> map = new HashMap<>(16);
|
||||
map.put("uploadPath", uploadPath);
|
||||
map.put("ocrResult", ocrResponse);
|
||||
return AjaxResult.success(Constants.UPLOAD_SUCCESS,map);
|
||||
}
|
||||
}
|
||||
|
|
@ -5,6 +5,3 @@ minio:
|
|||
access-key: minio
|
||||
secret-key: bonus@admin123
|
||||
bucket-name: smart-bid
|
||||
# 文件上传前缀
|
||||
uploadSuffix:
|
||||
main_database: mainDatabase #主体库
|
||||
|
|
@ -45,6 +45,16 @@ public class Constants
|
|||
*/
|
||||
public static final String SUCCESS = "0";
|
||||
|
||||
/**
|
||||
* 文件上传成功标识
|
||||
* */
|
||||
public static final String UPLOAD_SUCCESS = "上传成功";
|
||||
|
||||
/**
|
||||
* 文件上传失败标识
|
||||
* */
|
||||
public static final String UPLOAD_ERROR = "上传失败";
|
||||
|
||||
/**
|
||||
* 通用失败标识
|
||||
*/
|
||||
|
|
@ -170,4 +180,5 @@ public class Constants
|
|||
*/
|
||||
public static final String[] JOB_ERROR_STR = { "java.net.URL", "javax.naming.InitialContext", "org.yaml.snakeyaml",
|
||||
"org.springframework", "org.apache", "com.bonus.common.utils.file", "com.bonus.common.config", "com.bonus.generator" };
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,49 @@
|
|||
package com.bonus.file.enums;
|
||||
|
||||
public enum UploadSuffixEnum {
|
||||
/**
|
||||
* 主体库
|
||||
*/
|
||||
MAIN_DATABASE("main_database", "mainDatabase"),
|
||||
/**
|
||||
* 业绩库
|
||||
*/
|
||||
PERFORMANCE_DATABASE("performance_database", "performanceDatabase"),
|
||||
/**
|
||||
* 人员库
|
||||
*/
|
||||
PERSONNEL_DATABASE("personnel_database", "personnelDatabase"),
|
||||
/**
|
||||
* 财务库
|
||||
*/
|
||||
FINANCIAL_TREASURY("financial_treasury", "financialTreasury"),
|
||||
/**
|
||||
* 资质库
|
||||
*/
|
||||
QUALIFICATION_DATABASE("qualification_database", "qualificationDatabase"),
|
||||
/**
|
||||
* 业绩库
|
||||
*/
|
||||
TECHNICAL_SOLUTION_DATABASE("technical_solution_database", "technicalSolutionDatabase"),
|
||||
/**
|
||||
* 工器具库
|
||||
*/
|
||||
TOOLS_DATABASE("tools_database", "toolsDatabase");
|
||||
|
||||
private final String key;
|
||||
private final String value;
|
||||
|
||||
UploadSuffixEnum(String key, String value) {
|
||||
this.key = key;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public static String getValueByKey(String key) {
|
||||
for (UploadSuffixEnum suffix : values()) {
|
||||
if (suffix.key.equals(key)) {
|
||||
return suffix.value;
|
||||
}
|
||||
}
|
||||
return MAIN_DATABASE.value; // 默认值
|
||||
}
|
||||
}
|
||||
|
|
@ -27,7 +27,7 @@ public class FileUploadService {
|
|||
private MinioConfig minioConfig;
|
||||
|
||||
/**
|
||||
* 上传文件
|
||||
* 上传小文件 - 5M以内
|
||||
* @param file
|
||||
* @return
|
||||
*/
|
||||
|
|
@ -37,9 +37,9 @@ public class FileUploadService {
|
|||
return minioConfig.getBucketName();
|
||||
}catch (Exception e){
|
||||
log.error(e.toString(),e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除文件
|
||||
|
|
@ -57,7 +57,7 @@ public class FileUploadService {
|
|||
}
|
||||
|
||||
/**
|
||||
* 上传大文件-分片上传
|
||||
* 上传大文件-分片上传 - 5M以上
|
||||
* @param file
|
||||
* @return
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -41,4 +41,60 @@ public class FileUtil {
|
|||
}
|
||||
return filename.substring(filename.lastIndexOf("."));
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据MultipartFile获取MIME类型
|
||||
*/
|
||||
public static String getFileMimeType(MultipartFile file) {
|
||||
if (file == null || file.isEmpty()) {
|
||||
return "application/octet-stream";
|
||||
}
|
||||
|
||||
// 根据文件名后缀判断
|
||||
String originalFilename = file.getOriginalFilename();
|
||||
if (originalFilename != null) {
|
||||
return getMimeTypeByFilename(originalFilename);
|
||||
}
|
||||
|
||||
return "application/octet-stream";
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据文件名获取MIME类型
|
||||
*/
|
||||
private static String getMimeTypeByFilename(String filename) {
|
||||
if (filename == null) {
|
||||
return "application/octet-stream";
|
||||
}
|
||||
|
||||
String extension = filename.substring(filename.lastIndexOf(".") + 1).toLowerCase();
|
||||
|
||||
switch (extension) {
|
||||
case "png":
|
||||
return "image/png";
|
||||
case "jpg":
|
||||
case "jpeg":
|
||||
return "image/jpeg";
|
||||
case "gif":
|
||||
return "image/gif";
|
||||
case "bmp":
|
||||
return "image/bmp";
|
||||
case "webp":
|
||||
return "image/webp";
|
||||
case "pdf":
|
||||
return "application/pdf";
|
||||
case "doc":
|
||||
return "application/msword";
|
||||
case "docx":
|
||||
return "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
|
||||
case "xls":
|
||||
return "application/vnd.ms-excel";
|
||||
case "xlsx":
|
||||
return "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
|
||||
case "txt":
|
||||
return "text/plain";
|
||||
default:
|
||||
return "application/octet-stream";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,6 +16,10 @@ import org.springframework.web.multipart.MultipartFile;
|
|||
import javax.annotation.PostConstruct;
|
||||
import javax.annotation.Resource;
|
||||
import java.io.*;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.nio.file.StandardCopyOption;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Base64;
|
||||
import java.util.List;
|
||||
|
|
@ -504,33 +508,23 @@ public class MinioUtil {
|
|||
* @date 2025/10/16 17:32
|
||||
*/
|
||||
public File getFileFromMinio(String bucketName, String objectName) throws Exception {
|
||||
// 创建临时目录(如果不存在)
|
||||
File tempDir = new File(System.getProperty("java.io.tmpdir"), "minio_downloads");
|
||||
if (!tempDir.exists()) {
|
||||
tempDir.mkdirs();
|
||||
}
|
||||
Path tempDir = Paths.get(System.getProperty("java.io.tmpdir"), "minio_downloads");
|
||||
Files.createDirectories(tempDir);
|
||||
|
||||
String safeFileName = getSafeFileName(objectName);
|
||||
File tempFile = new File(tempDir, "minio_" + System.currentTimeMillis() + "_" + safeFileName);
|
||||
|
||||
String fileName = "minio_" + System.currentTimeMillis() + "_" + safeFileName;
|
||||
Path tempFilePath = tempDir.resolve(fileName);
|
||||
try (InputStream stream = minioClient.getObject(
|
||||
GetObjectArgs.builder()
|
||||
.bucket(bucketName)
|
||||
.object(objectName)
|
||||
.build());
|
||||
FileOutputStream outputStream = new FileOutputStream(tempFile)) {
|
||||
.build())) {
|
||||
|
||||
byte[] buffer = new byte[8192];
|
||||
int bytesRead;
|
||||
while ((bytesRead = stream.read(buffer)) != -1) {
|
||||
outputStream.write(buffer, 0, bytesRead);
|
||||
}
|
||||
Files.copy(stream, tempFilePath, StandardCopyOption.REPLACE_EXISTING);
|
||||
return tempFilePath.toFile();
|
||||
|
||||
return tempFile;
|
||||
} catch (Exception e) {
|
||||
if (tempFile.exists()) {
|
||||
tempFile.delete();
|
||||
}
|
||||
Files.deleteIfExists(tempFilePath);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue