系统集成 minio
This commit is contained in:
parent
e37c6c1370
commit
23747f5f0a
|
|
@ -1,20 +1,23 @@
|
|||
package com.bonus.web.controller.file;
|
||||
|
||||
import com.bonus.common.core.domain.AjaxResult;
|
||||
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.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.multipart.MultipartFile;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* @className:FileUploadController
|
||||
|
|
@ -30,15 +33,40 @@ public class FileUploadController {
|
|||
@Resource(name = "FileUploadService")
|
||||
private FileUploadService fileUploadService;
|
||||
|
||||
@Resource(name = "OcrService")
|
||||
private OcrService ocrService;
|
||||
|
||||
@Value("${uploadSuffix.main_database}")
|
||||
private String uploadSuffix;
|
||||
|
||||
@Resource
|
||||
private MinioConfig minioConfig;
|
||||
|
||||
@Resource
|
||||
private MinioUtil minioUtil;
|
||||
|
||||
|
||||
@PostMapping(value = "uploadFile")
|
||||
private AjaxResult ocrHandler(MultipartFile file) {
|
||||
private AjaxResult uploadFile(@RequestParam(name = "file",required = false) MultipartFile file) throws Exception {
|
||||
// 生成文件路径
|
||||
String uploadPath = FileUtil.generateDatePath(file, uploadSuffix);
|
||||
String s = fileUploadService.uploadFile(file,uploadPath);
|
||||
System.err.println("文件路径" + s);
|
||||
String uploadPath = FileUtil.generateDatePath(file, uploadSuffix).replace("\\", "/");
|
||||
fileUploadService.uploadFile(file,uploadPath);
|
||||
System.err.println("文件路径----" + uploadPath);
|
||||
|
||||
// 获取临时路径
|
||||
File fileFromMinio = minioUtil.getFileFromMinio(minioConfig.getBucketName(), "mainDatabase/2025/10/16/75b562e0-0a2f-4453-bcc9-f77cee28ca0e.png");
|
||||
OcrRequest ocrRequest = new OcrRequest();
|
||||
ocrRequest.setFile(fileFromMinio);
|
||||
ocrRequest.setType("image/png");
|
||||
ocrRequest.setFields_json("[\"姓名\", \"公民身份号码\"]");
|
||||
OcrResponse ocrResponse = ocrService.callOcrService(ocrRequest);
|
||||
return AjaxResult.success(ocrResponse);
|
||||
}
|
||||
|
||||
@PostMapping(value = "getFile")
|
||||
private AjaxResult getFile() {
|
||||
SysFile file = fileUploadService.getFile("mainDatabase/2025/10/16/75b562e0-0a2f-4453-bcc9-f77cee28ca0e.png");
|
||||
System.err.println("文件对象:{}" + file);
|
||||
return AjaxResult.success();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,4 +7,4 @@ minio:
|
|||
bucket-name: smart-bid
|
||||
# 文件上传前缀
|
||||
uploadSuffix:
|
||||
main_database: main_database #主体库
|
||||
main_database: mainDatabase #主体库
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
spring:
|
||||
profiles:
|
||||
active: @profiles.active@,druid,ocr
|
||||
active: @profiles.active@,druid,ocr,file
|
||||
# 解决SpringBoot 2.6+与SpringFox兼容性问题
|
||||
mvc:
|
||||
pathmatch:
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ import lombok.Builder;
|
|||
import lombok.Data;
|
||||
import org.apache.commons.lang3.builder.ToStringBuilder;
|
||||
import org.apache.commons.lang3.builder.ToStringStyle;
|
||||
import org.apache.ibatis.type.Alias;
|
||||
|
||||
/**
|
||||
* 文件信息
|
||||
|
|
@ -12,6 +13,7 @@ import org.apache.commons.lang3.builder.ToStringStyle;
|
|||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@Alias("ServerSysFile")
|
||||
public class SysFile
|
||||
{
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@ import org.springframework.stereotype.Service;
|
|||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.io.File;
|
||||
|
||||
/**
|
||||
* @className:FileUploadService
|
||||
|
|
@ -57,7 +58,7 @@ public class FileUploadService {
|
|||
}
|
||||
|
||||
/**
|
||||
* 上传大文件
|
||||
* 上传大文件-分片上传
|
||||
* @param file
|
||||
* @return
|
||||
*/
|
||||
|
|
@ -70,4 +71,28 @@ public class FileUploadService {
|
|||
return null;
|
||||
}
|
||||
|
||||
public SysFile getFile(String filePath) {
|
||||
try{
|
||||
String fileUrl = minioUtil.getFileUrl(minioConfig.getBucketName(), filePath, 60 * 60 * 12);
|
||||
String lsUrl= fileUrl.replace(minioConfig.getEndpoint(),minioConfig.getUrl());
|
||||
String url = minioConfig.getUrl() + File.separator + filePath;
|
||||
log.info("临时路径:{}",lsUrl);
|
||||
return SysFile.builder()
|
||||
.name("文件名称")
|
||||
.url(url).build();
|
||||
}catch (Exception e){
|
||||
log.error(e.toString(),e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public SysFile getFile2(String filePath) {
|
||||
try{
|
||||
minioUtil.getFileUrl(null,null);
|
||||
}catch (Exception e){
|
||||
log.error(e.toString(),e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -28,7 +28,7 @@ public class FileUtil {
|
|||
String fileExtension = getFileExtension(originalFilename);
|
||||
String uniqueFileName = UUID.randomUUID().toString() + fileExtension;
|
||||
|
||||
// 构建完整路径
|
||||
// 构建完整路径:baseDir/年/月/日/UUID.扩展名
|
||||
return Paths.get(baseDir, datePath, uniqueFileName).toString();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -15,9 +15,7 @@ import org.springframework.web.multipart.MultipartFile;
|
|||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.annotation.Resource;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.InputStream;
|
||||
import java.io.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Base64;
|
||||
import java.util.List;
|
||||
|
|
@ -296,13 +294,13 @@ public class MinioUtil {
|
|||
}
|
||||
|
||||
/**
|
||||
* 获取文件bast64
|
||||
* 获取文件base64
|
||||
* @param bucketName
|
||||
* @param path
|
||||
* @return
|
||||
*/
|
||||
@SneakyThrows(Exception.class)
|
||||
public String getMinioBast64(String bucketName,String path) {
|
||||
public String getMinioBase64(String bucketName,String path) {
|
||||
InputStream inputStream= minioClient.getObject(GetObjectArgs.builder().bucket(bucketName).object(path).build());
|
||||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||
byte[] buffer = new byte[1024];
|
||||
|
|
@ -497,5 +495,74 @@ public class MinioUtil {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成File临时对象
|
||||
* @param bucketName
|
||||
* @param objectName
|
||||
* @return File
|
||||
* @author cwchen
|
||||
* @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();
|
||||
}
|
||||
|
||||
String safeFileName = getSafeFileName(objectName);
|
||||
File tempFile = new File(tempDir, "minio_" + System.currentTimeMillis() + "_" + safeFileName);
|
||||
|
||||
try (InputStream stream = minioClient.getObject(
|
||||
GetObjectArgs.builder()
|
||||
.bucket(bucketName)
|
||||
.object(objectName)
|
||||
.build());
|
||||
FileOutputStream outputStream = new FileOutputStream(tempFile)) {
|
||||
|
||||
byte[] buffer = new byte[8192];
|
||||
int bytesRead;
|
||||
while ((bytesRead = stream.read(buffer)) != -1) {
|
||||
outputStream.write(buffer, 0, bytesRead);
|
||||
}
|
||||
|
||||
return tempFile;
|
||||
} catch (Exception e) {
|
||||
if (tempFile.exists()) {
|
||||
tempFile.delete();
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取安全的文件名,移除路径和非法字符
|
||||
*/
|
||||
private String getSafeFileName(String objectName) {
|
||||
if (objectName == null || objectName.isEmpty()) {
|
||||
return "unknown_file";
|
||||
}
|
||||
|
||||
// 提取文件名(去掉路径)
|
||||
String fileName = objectName;
|
||||
fileName = fileName.substring(fileName.lastIndexOf("/") + 1);
|
||||
fileName = fileName.substring(fileName.lastIndexOf("\\") + 1);
|
||||
|
||||
// 移除Windows文件名中的非法字符
|
||||
fileName = fileName.replaceAll("[\\\\/:*?\"<>|]", "_");
|
||||
|
||||
// 限制文件名长度(避免文件名过长)
|
||||
if (fileName.length() > 100) {
|
||||
int dotIndex = fileName.lastIndexOf(".");
|
||||
if (dotIndex > 0) {
|
||||
String name = fileName.substring(0, Math.min(50, dotIndex));
|
||||
String extension = fileName.substring(dotIndex);
|
||||
fileName = name + extension;
|
||||
} else {
|
||||
fileName = fileName.substring(0, 50);
|
||||
}
|
||||
}
|
||||
|
||||
return fileName.isEmpty() ? "minio_file" : fileName;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -117,6 +117,13 @@ public class OcrService {
|
|||
log.error("调用OCR服务失败", e);
|
||||
throw new IOException("OCR服务调用失败: " + e.getMessage(), e);
|
||||
} finally {
|
||||
// 清理临时文件
|
||||
if (ocrRequest.getFile() != null && ocrRequest.getFile().exists()) {
|
||||
boolean deleted = ocrRequest.getFile().delete();
|
||||
if (!deleted) {
|
||||
log.warn("临时文件删除失败: {}", ocrRequest.getFile().getAbsolutePath());
|
||||
}
|
||||
}
|
||||
httpPost.releaseConnection();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue