public boolean photoSave(CarIdentifyData carIdentifyData){
List<String> strings = new ArrayList<>();
strings.add(carIdentifyData.getCarPhoto());
strings.add(carIdentifyData.getCarNumberPhoto());
// base64转文件
MultipartFile[] Files =base64ToMultipartUtils.base64ToMultipartFile(strings);
// 车辆图片和车牌图片上传minio
List<String> urlList = base64ToMultipartUtils.upload(Files);
if (ObjectUtils.isNotEmpty(urlList)) {
carIdentifyData.setCarPhoto(urlList.get(0));
carIdentifyData.setCarNumberPhoto(urlList.get(1));
super.save(carIdentifyData);
return true;
}else {
return false;
}
}
base64转文件:
public MultipartFile[] base64ToMultipartFile(List<String> listBase64){
MultipartFile[] files = new MultipartFile[2];
try {
for (int i = 0; i < listBase64.size(); i++) {
Base64.Decoder decoder = Base64.getMimeDecoder();
// 去掉base64前缀 data:image/jpeg;base64,
byte[] b = decoder.decode(listBase64.get(i));
// 处理数据
for (int j = 0; j < b.length; ++j) {
if (b[j] < 0) {
b[j] += 256;
}
}
InputStream inputStream = new ByteArrayInputStream(b);
String string = UUID.randomUUID().toString().concat(".png");
MultipartFile file = new MockMultipartFile(string,string,ContentType.IMAGE_PNG.toString(), inputStream);
if(ObjectUtils.isNotEmpty(file)){
files[i] = file;
}
inputStream.close();
}
return files;
} catch (Exception e) {
log.info("base64ToMultipartFile - {}", e.getStackTrace());
return files;
}
}
图片上传minio:
public List<String> upload(MultipartFile[] file) {
List<String> objectNameList = minioUtil.upload(file);
List<String> urlList = new ArrayList<>();
if (null != objectNameList && objectNameList.size() >0) {
for (String objectName : objectNameList) {
urlList.add(minioPath + objectName);
}
return urlList;
}
return new ArrayList<>();
}
minio多文件上传工具类:
public List<String> upload(MultipartFile[] files) {标签:return,minio,base64,List,file,urlList,new,MultipartFile,上传 From: https://www.cnblogs.com/heyi-77/p/17266266.html
List<String> objectNameList = new ArrayList<>();
for (MultipartFile file : files){
String originalFilename = file.getOriginalFilename();
if (StringUtils.isBlank(originalFilename)){
throw new RuntimeException();
}
String fileName = UuidUtils.generateUuid() + originalFilename.substring(originalFilename.lastIndexOf("."));
String objectName = fileName;
try {
PutObjectArgs objectArgs = PutObjectArgs.builder().bucket(prop.getBucketName()).object(objectName)
.stream(file.getInputStream(), file.getSize(), -1).contentType(file.getContentType()).build();
//文件名称相同会覆盖
minioClient.putObject(objectArgs);
} catch (Exception e) {
e.printStackTrace();
return null;
}
objectNameList.add(objectName);
}
return objectNameList;
}