优化二维码生成逻辑,在重新生成二维码时,将阿里云上已经存在的二维码删除,然后重新生成
This commit is contained in:
parent
f21bbe03c7
commit
cbdbbcd7b6
@ -2,14 +2,21 @@ package com.xhpc.charging.station.utils.img;
|
||||
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import com.aliyun.oss.OSSClient;
|
||||
import com.aliyun.oss.model.*;
|
||||
import com.xhpc.charging.station.mapper.XhpcImgMapper;
|
||||
import com.xhpc.common.domain.XhpcTerminal;
|
||||
import org.springframework.core.env.Environment;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URLDecoder;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static com.aliyun.oss.internal.OSSConstants.URL_ENCODING;
|
||||
|
||||
public class QrImgUtils {
|
||||
|
||||
/**
|
||||
@ -53,6 +60,9 @@ public class QrImgUtils {
|
||||
//4.上传图片至服务器
|
||||
// 创建OSSClient实例
|
||||
OSSClient ossClient = new OSSClient(environment.getProperty("oss.endpoint"), environment.getProperty("oss.access-key"), environment.getProperty("oss.secret-key"));
|
||||
// 删除阿里云上面之前可能存在的桩图片文件夹
|
||||
String pileNumber = serialNumber.substring(0, 14);
|
||||
clearRepetition(ossClient, environment, pileNumber);
|
||||
// 上传文件流
|
||||
ossClient.putObject(environment.getProperty("oss.bucket-name"), xhpcTerminal.getPileSerialNumber() + "/" + finallyImgFileName, new File(environment.getProperty("destPath") + File.separatorChar + finallyImgFileName));
|
||||
ossClient.shutdown();
|
||||
@ -76,4 +86,49 @@ public class QrImgUtils {
|
||||
System.out.println("二维码生成图片失败,请检查生成二维码所需要的资源路径是否正确");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 清理阿里云中冗余的二维码图片数据
|
||||
* <p>
|
||||
* 该方法会根据传入的桩名称<param>prefix<param>,
|
||||
* 到阿里云上去删除指定的桩下面的所有终端二维码(包括其本身)
|
||||
*
|
||||
* @param ossClient 阿里云oss存储对象
|
||||
* @param environment nacos环境对象
|
||||
* @param prefix 桩的名称
|
||||
*/
|
||||
public static void clearRepetition(OSSClient ossClient, Environment environment, String prefix) {
|
||||
|
||||
String bucketName = environment.getProperty("bucket-name");
|
||||
System.out.println("==》桩:" + prefix + "图片冗余正在清理");
|
||||
prefix = prefix + "/";
|
||||
String nextMarker = null;
|
||||
ObjectListing objectListing = null;
|
||||
do {
|
||||
ListObjectsRequest listObjectsRequest = new ListObjectsRequest(bucketName)
|
||||
.withPrefix(prefix)
|
||||
.withMarker(nextMarker);
|
||||
objectListing = ossClient.listObjects(listObjectsRequest);
|
||||
if (objectListing.getObjectSummaries().size() > 0) {
|
||||
List<String> keys = new ArrayList<String>();
|
||||
for (OSSObjectSummary s : objectListing.getObjectSummaries()) {
|
||||
keys.add(s.getKey());
|
||||
}
|
||||
DeleteObjectsRequest deleteObjectsRequest = new DeleteObjectsRequest(bucketName).withKeys(keys).withEncodingType(URL_ENCODING);
|
||||
DeleteObjectsResult deleteObjectsResult = ossClient.deleteObjects(deleteObjectsRequest);
|
||||
List<String> deletedObjects = deleteObjectsResult.getDeletedObjects();
|
||||
try {
|
||||
for (String obj : deletedObjects) {
|
||||
String deleteObj = URLDecoder.decode(obj, "UTF-8");
|
||||
System.out.println(deleteObj);
|
||||
}
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
nextMarker = objectListing.getNextMarker();
|
||||
} while (objectListing.isTruncated());
|
||||
System.out.println(DateUtil.now() + "==》图片冗余清理执行成功");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user