兴安优化车辆包兼容
This commit is contained in:
@@ -13,24 +13,19 @@ import com.dcsoft.common.core.utils.file.FileUtils;
|
||||
import com.dcsoft.common.redis.service.RedisService;
|
||||
import com.dcsoft.file.service.IFaceService;
|
||||
import com.dcsoft.file.service.ISysFileService;
|
||||
import com.dcsoft.file.utils.FileUploadUtils;
|
||||
import com.dcsoft.file.utils.ImageToBase64Utils;
|
||||
import com.dcsoft.file.utils.MinioUtil;
|
||||
import com.dcsoft.file.utils.PictureUtils;
|
||||
import com.dcsoft.file.utils.*;
|
||||
import com.dcsoft.system.api.domain.SysFile;
|
||||
import io.minio.MinioClient;
|
||||
import io.minio.RemoveObjectArgs;
|
||||
import net.coobird.thumbnailator.Thumbnails;
|
||||
import net.coobird.thumbnailator.geometry.Positions;
|
||||
import org.apache.commons.codec.binary.Base64;
|
||||
import org.apache.commons.codec.digest.DigestUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
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 java.io.File;
|
||||
@@ -164,6 +159,27 @@ public class SysFileController {
|
||||
}
|
||||
}
|
||||
|
||||
@PostMapping("uploadMinioCarBase64")
|
||||
public R<SysFile> uploadMinioCarBase64(@RequestBody JSONObject data) {
|
||||
try {
|
||||
String url = data.getString("url");
|
||||
if(StringUtils.isEmpty(url)){
|
||||
return R.fail("图片地址不能为空");
|
||||
}
|
||||
MultipartFile file = Base64ToMultipartFileUtil.convertToMultipartFile(url);
|
||||
|
||||
String fileNames = FileUploadUtils.uploadMinio(file, this.bucketName2, file.getName());
|
||||
SysFile sysFile = new SysFile();
|
||||
sysFile.setName(FileUtils.getName(fileNames));
|
||||
fileNames = fileNames.replace(miniourl, miniogwurl);
|
||||
sysFile.setUrl(fileNames);
|
||||
return R.ok(sysFile);
|
||||
} catch (Exception e) {
|
||||
log.error("上传文件失败", e);
|
||||
return R.fail(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 文件上传请求
|
||||
@@ -293,29 +309,16 @@ public class SysFileController {
|
||||
@PostMapping("uploadMinio1")
|
||||
public R<SysFile> uploadMinio1(MultipartFile file, String fileName) {
|
||||
try {
|
||||
//判断压缩图片
|
||||
if ((1024 * 1024 * 0.1) <= file.getSize()) {
|
||||
// 小于 1M 的
|
||||
try {
|
||||
String fileNames1 = file.getOriginalFilename();
|
||||
File newFile = new File(fileNames1);
|
||||
if ((1024 * 1024 * 0.1) <= file.getSize() && file.getSize() <= (1024 * 1024)) {
|
||||
Thumbnails.of(file.getInputStream()).scale(1f).outputQuality(0.4f).toFile(newFile);
|
||||
}
|
||||
// 1 - 2M 的
|
||||
else if ((1024 * 1024) < file.getSize() && file.getSize() <= (1024 * 1024 * 2)) {
|
||||
Thumbnails.of(file.getInputStream()).scale(1f).outputQuality(0.2f).toFile(newFile);
|
||||
}
|
||||
// 2M 以上的
|
||||
else if ((1024 * 1024 * 2) < file.getSize()) {
|
||||
Thumbnails.of(file.getInputStream()).scale(1f).outputQuality(0.1f).toFile(newFile);
|
||||
}
|
||||
// 转为 MultipartFile
|
||||
file = PictureUtils.getMultipartFile(newFile);
|
||||
newFile.delete();
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
try {
|
||||
// 裁剪图片为640*480
|
||||
String originalFilename = file.getOriginalFilename();
|
||||
File newFile = new File(originalFilename);
|
||||
Thumbnails.of(file.getInputStream()).crop(Positions.CENTER).size(480, 640).toFile(newFile);
|
||||
|
||||
file = PictureUtils.getMultipartFile(newFile);
|
||||
newFile.delete();
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
initFlowRules();
|
||||
//上传前进行人脸检测
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
package com.dcsoft.file.utils;
|
||||
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.Base64;
|
||||
|
||||
public class Base64MultipartFile implements MultipartFile {
|
||||
|
||||
private final byte[] fileContent;
|
||||
private final String fileName;
|
||||
private final String contentType;
|
||||
|
||||
public Base64MultipartFile(String base64Data, String fileName) {
|
||||
// 清理Base64数据
|
||||
String cleanedBase64 = base64Data.contains(",")
|
||||
? base64Data.substring(base64Data.indexOf(",") + 1)
|
||||
: base64Data;
|
||||
|
||||
this.fileContent = Base64.getDecoder().decode(cleanedBase64);
|
||||
this.fileName = fileName;
|
||||
this.contentType = extractContentType(base64Data);
|
||||
}
|
||||
|
||||
private String extractContentType(String base64Data) {
|
||||
if (base64Data.startsWith("data:image/jpeg")) {
|
||||
return "image/jpeg";
|
||||
} else if (base64Data.startsWith("data:image/png")) {
|
||||
return "image/png";
|
||||
} else if (base64Data.startsWith("data:image/gif")) {
|
||||
return "image/gif";
|
||||
}
|
||||
return "application/octet-stream";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "file";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getOriginalFilename() {
|
||||
return fileName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getContentType() {
|
||||
return contentType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return fileContent == null || fileContent.length == 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getSize() {
|
||||
return fileContent.length;
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] getBytes() throws IOException {
|
||||
return fileContent;
|
||||
}
|
||||
|
||||
@Override
|
||||
public InputStream getInputStream() throws IOException {
|
||||
return new ByteArrayInputStream(fileContent);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void transferTo(File dest) throws IOException, IllegalStateException {
|
||||
try (FileOutputStream fos = new FileOutputStream(dest)) {
|
||||
fos.write(fileContent);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.dcsoft.file.utils;
|
||||
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
public class Base64ToMultipartFileUtil {
|
||||
|
||||
public static MultipartFile convertToMultipartFile(String base64Data, String fileName) {
|
||||
return new Base64MultipartFile(base64Data, fileName);
|
||||
}
|
||||
|
||||
/**
|
||||
* 自动生成文件名
|
||||
*/
|
||||
public static MultipartFile convertToMultipartFile(String base64Data) {
|
||||
String fileName = generateFileName(base64Data);
|
||||
return convertToMultipartFile(base64Data, fileName);
|
||||
}
|
||||
|
||||
private static String generateFileName(String base64Data) {
|
||||
String extension = ".jpg";
|
||||
if (base64Data.startsWith("data:image/png")) {
|
||||
extension = ".png";
|
||||
} else if (base64Data.startsWith("data:image/gif")) {
|
||||
extension = ".gif";
|
||||
}
|
||||
return "image_" + System.currentTimeMillis() + extension;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user